﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;


public partial class Main_ServiceInvoice_SaleInvoiceEdit : System.Web.UI.Page
{
    Datautility DU = new Datautility();

    #region Declare list ControlID

    int counter = 1;

    List<string> listcounter;

    List<string> listtdservicectrl;
    List<string> listddlservice;

    List<string> listhidTaxableNonTaxable;
    List<string> listhidserviceLid;

    List<string> listtddescriptionctrl;
    List<string> listtxtdescriptionctrl;

    List<string> listtdyearrefctrl;
    List<string> listtxtyearrefctrl;

    List<string> listtduomctrl;
    List<string> listtxtuomctrl;

    List<string> listtdunit2ctrl;
    List<string> listtxtunit2ctrl;
    List<string> listtdunit3ctrl;
    List<string> listtxtunit3ctrl;
    List<string> listtdunit4ctrl;
    List<string> listtxtunit4ctrl;

    List<string> listtdunitrate1ctrl;
    List<string> listtxtunitrate1ctrl;
    List<string> listtdunitrate2ctrl;
    List<string> listtxtunitrate2ctrl;

    List<string> listtdgrossamtctrl;
    List<string> listtxtgrossamtctrl;

    List<string> listtddiscount1ctrl;
    List<string> listtxtdiscount1ctrl;
    List<string> listtddiscount2ctrl;
    List<string> listtxtdiscount2ctrl;

    List<string> listtdservicecharge1crtl;
    List<string> listtxtservicecharge1crtl;
    List<string> listhidServiceCharge;
    List<string> listtdservicecharge2crtl;
    List<string> listtxtservicecharge2crtl;
    List<string> listtdservicecharge3crtl;
    List<string> listtxtservicecharge3crtl;

    List<string> listtdserviceTax1crtl;
    List<string> listtxtserviceTax1crtl;
    List<string> listtdserviceTax2crtl;
    List<string> listtxtserviceTax2crtl;
    List<string> listtdserviceTax3crtl;
    List<string> listtxtserviceTax3crtl;
    List<string> listtdserviceTax4crtl;
    List<string> listtxtserviceTax4crtl;
    List<string> listtdserviceTax5crtl;
    List<string> listtxtserviceTax5crtl;

    List<string> listtdTaxincGrandTotalctrl;
    List<string> listtxtTaxincGrandTotalctrl;
    List<string> LstChkDelete;
    List<string> LsthidChildID;
    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        OverrideAddMore();
    }

    #endregion End Declare ControlID



    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            #region Initilize ControlID

            listcounter = new List<string>();
            ViewState["listcounter"] = listcounter;

            listtdservicectrl = new List<string>();
            ViewState["listtdservicectrl"] = listtdservicectrl;
            listddlservice = new List<string>();
            ViewState["listddlservice"] = listddlservice;

            listhidTaxableNonTaxable = new List<string>();
            ViewState["listhidTaxableNonTaxable"] = listhidTaxableNonTaxable;

            listhidserviceLid = new List<string>();
            ViewState["listhidserviceLid"] = listhidserviceLid;


            listtddescriptionctrl = new List<string>();
            ViewState["listtddescriptionctrl"] = listtddescriptionctrl;
            listtxtdescriptionctrl = new List<string>();
            ViewState["listtxtdescriptionctrl"] = listtxtdescriptionctrl;

            listtdyearrefctrl = new List<string>();
            ViewState["listtdyearrefctrl"] = listtdyearrefctrl;
            listtxtyearrefctrl = new List<string>();
            ViewState["listtxtyearrefctrl"] = listtxtyearrefctrl;

            listtduomctrl = new List<string>();
            ViewState["listtduomctrl"] = listtduomctrl;
            listtxtuomctrl = new List<string>();
            ViewState["listtxtuomctrl"] = listtxtuomctrl;

            listtdunit2ctrl = new List<string>();
            ViewState["listtdunit2ctrl"] = listtdunit2ctrl;
            listtxtunit2ctrl = new List<string>();
            ViewState["listtxtunit2ctrl"] = listtxtunit2ctrl;
            listtdunit3ctrl = new List<string>();
            ViewState["listtdunit3ctrl"] = listtdunit3ctrl;
            listtxtunit3ctrl = new List<string>();
            ViewState["listtxtunit3ctrl"] = listtxtunit3ctrl;
            listtdunit4ctrl = new List<string>();
            ViewState["listtdunit4ctrl"] = listtdunit4ctrl;
            listtxtunit4ctrl = new List<string>();
            ViewState["listtxtunit4ctrl"] = listtxtunit4ctrl;

            listtdunitrate1ctrl = new List<string>();
            ViewState["listtdunitrate1ctrl"] = listtdunitrate1ctrl;
            listtxtunitrate1ctrl = new List<string>();
            ViewState["listtxtunitrate1ctrl"] = listtxtunitrate1ctrl;
            listtdunitrate2ctrl = new List<string>();
            ViewState["listtdunitrate2ctrl"] = listtdunitrate2ctrl;
            listtxtunitrate2ctrl = new List<string>();
            ViewState["listtxtunitrate2ctrl"] = listtxtunitrate2ctrl;

            listtdgrossamtctrl = new List<string>();
            ViewState["listtdgrossamtctrl"] = listtdgrossamtctrl;
            listtxtgrossamtctrl = new List<string>();
            ViewState["listtxtgrossamtctrl"] = listtxtgrossamtctrl;

            listtddiscount1ctrl = new List<string>();
            ViewState["listtddiscount1ctrl"] = listtddiscount1ctrl;
            listtxtdiscount1ctrl = new List<string>();
            ViewState["listtxtdiscount1ctrl"] = listtxtdiscount1ctrl;
            listtddiscount2ctrl = new List<string>();
            ViewState["listtddiscount2ctrl"] = listtddiscount2ctrl;
            listtxtdiscount2ctrl = new List<string>();
            ViewState["listtxtdiscount2ctrl"] = listtxtdiscount2ctrl;

            listtdservicecharge1crtl = new List<string>();
            ViewState["listtdservicecharge1crtl"] = listtdservicecharge1crtl;
            listtxtservicecharge1crtl = new List<string>();
            ViewState["listtxtservicecharge1crtl"] = listtxtservicecharge1crtl;
            listhidServiceCharge = new List<string>();
            ViewState["listhidServiceCharge"] = listhidServiceCharge;
            listtdservicecharge2crtl = new List<string>();
            ViewState["listtdservicecharge2crtl"] = listtdservicecharge2crtl;
            listtxtservicecharge2crtl = new List<string>();
            ViewState["listtxtservicecharge2crtl"] = listtxtservicecharge2crtl;
            listtdservicecharge3crtl = new List<string>();
            ViewState["listtdservicecharge3crtl"] = listtdservicecharge3crtl;
            listtxtservicecharge3crtl = new List<string>();
            ViewState["listtxtservicecharge3crtl"] = listtxtservicecharge3crtl;

            listtdserviceTax1crtl = new List<string>();
            ViewState["listtdserviceTax1crtl"] = listtdserviceTax1crtl;
            listtxtserviceTax1crtl = new List<string>();
            ViewState["listtxtserviceTax1crtl"] = listtxtserviceTax1crtl;
            listtdserviceTax2crtl = new List<string>();
            ViewState["listtdserviceTax2crtl"] = listtdserviceTax2crtl;
            listtxtserviceTax2crtl = new List<string>();
            ViewState["listtxtserviceTax2crtl"] = listtxtserviceTax2crtl;
            listtdserviceTax3crtl = new List<string>();
            ViewState["listtdserviceTax3crtl"] = listtdserviceTax3crtl;
            listtxtserviceTax3crtl = new List<string>();
            ViewState["listtxtserviceTax3crtl"] = listtxtserviceTax3crtl;
            listtdserviceTax4crtl = new List<string>();
            ViewState["listtdserviceTax4crtl"] = listtdserviceTax4crtl;
            listtxtserviceTax4crtl = new List<string>();
            ViewState["listtxtserviceTax4crtl"] = listtxtserviceTax4crtl;
            listtdserviceTax5crtl = new List<string>();
            ViewState["listtdserviceTax5crtl"] = listtdserviceTax5crtl;
            listtxtserviceTax5crtl = new List<string>();
            ViewState["listtxtserviceTax5crtl"] = listtxtserviceTax5crtl;

            listtdTaxincGrandTotalctrl = new List<string>();
            ViewState["listtdTaxincGrandTotalctrl"] = listtdTaxincGrandTotalctrl;
            listtxtTaxincGrandTotalctrl = new List<string>();
            ViewState["listtxtTaxincGrandTotalctrl"] = listtxtTaxincGrandTotalctrl;

            LstChkDelete = new List<string>();
            ViewState["LstChkDelete"] = LstChkDelete;//LsthidChildID

            LsthidChildID = new List<string>();
            ViewState["LsthidChildID"] = LsthidChildID;

            #endregion End Initilize ControlID

            BindCustomer();
            ddlFormat.Enabled = false;
            txtPrefix.Enabled = false;
            txtSuffix.Enabled = false;
            txtbillno.Enabled = false;
            txtdate.Enabled = false;
            btnDate.Visible = false;
            btnDate.Enabled = false;
            BindFormat();
        }
        if (!IsPostBack)
        {
            BindDetailsForEdit();
            BindChildDetailEdit();
            PostBilling();
            BindTax();
        }
    }
    protected void BindTax()
    {
        string caldate = "";
        if (txtdate.Text.ToString() != string.Empty)
        {
            string str = txtdate.Text.ToString();
            string[] s = str.Split('/');
            caldate = s[1] + "/" + s[0] + "/" + s[2].ToString();
        }

        SqlParameter[] parmtax = { 
                                   new SqlParameter("@CURRENTDATE",caldate)
                                 };
        DataTable dtSerTax = DU.getdatausingprocedure("USP_IVC_GetCurrentServiceTaxRate", parmtax);
        if (dtSerTax.Rows.Count > 0)
        {
            //txtservicetaxpercent.Text = dtSerTax.Rows[0]["SERVICETAX"].ToString();
            //txtpecesspercent.Text = dtSerTax.Rows[0]["PCESS"].ToString();
            //txthecesspercent.Text = dtSerTax.Rows[0]["HCESS"].ToString();

            hidservicetaxpercent.Value = dtSerTax.Rows[0]["SERVICETAX_LID"].ToString();
            hidpecesspercent.Value = dtSerTax.Rows[0]["PCESS_LID"].ToString();
            hidhecesspercent.Value = dtSerTax.Rows[0]["HCESS_LID"].ToString();
        }
    }
    protected void BindFormat()
    {
        SqlParameter[] param = { 
                                 new SqlParameter("@TRANSACTIONTYPE","1")
                               };

        DataTable dtformate = DU.getdatausingprocedure("USP_IVC_GetAllTransactionFormatNameID", param);

        if (dtformate.Rows.Count > 0)
        {
            ddlFormat.DataSource = dtformate;
            ddlFormat.DataTextField = "FORMATNAME";
            ddlFormat.DataValueField = "FORMATID";
            ddlFormat.DataBind();
        }
    }
    protected void PostBilling()
    {
        if (ddlFormat.SelectedValue != "0")
        {
            hidamtcess.Value = string.Empty;
            hidtdsrate.Value = string.Empty;
            hidcess1rate.Value = string.Empty;
            hidcess2rate.Value = string.Empty;
            hidsurchargerate.Value = string.Empty;
            hideffectiverate.Value = string.Empty;
            SqlParameter[] parma = { 
                                    new SqlParameter("@FORMATID",ddlFormat.SelectedValue)
                                   };
            DataTable dtPostBilling = DU.getdatausingprocedure("USP_IVC_GetTransactionFormat_PostBilling", parma);
            if (dtPostBilling.Rows.Count > 0)
            {
                hidamtcess.Value = String.Format("{0:0.00}", dtPostBilling.Rows[0]["AMTCESS"]);
                hidtdsrate.Value = String.Format("{0:0.00}", dtPostBilling.Rows[0]["TDSRATE"]);
                hidcess1rate.Value = String.Format("{0:0.00}", dtPostBilling.Rows[0]["CESS1RATE"]);
                hidcess2rate.Value = String.Format("{0:0.00}", dtPostBilling.Rows[0]["CESS2RATE"]);
                hidsurchargerate.Value = String.Format("{0:0.00}", dtPostBilling.Rows[0]["SURCHARGERATE"]);
                hideffectiverate.Value = String.Format("{0:0.00}", dtPostBilling.Rows[0]["EFFECTIVERATE"]);

                txteffectiveratecurr.Text = String.Format("{0:0.00}", dtPostBilling.Rows[0]["EFFECTIVERATE"]);
                txteffectiveratepre.Text = String.Format("{0:0.00}", dtPostBilling.Rows[0]["EFFECTIVERATE"]);

            }
        }
    }
    protected void BindCustomer()
    {
        DataTable dtCustomer = DU.getdatausingprocedure("USP_IVC_GetCustomerLedger");
        if (dtCustomer.Rows.Count > 0)
        {
            ddlCustomer.DataSource = dtCustomer;
            ddlCustomer.DataTextField = "LNAME";
            ddlCustomer.DataValueField = "LID";
            ddlCustomer.DataBind();

            ListItem li = new ListItem("-- Select Customer --", "0");
            ddlCustomer.Items.Insert(0, li);
            li.Selected = true;
        }
    }
    protected void ddlCustomer_SelectedIndex(object sender, EventArgs e)
    {
        txtaddress.Text = string.Empty;
        if (ddlCustomer.SelectedValue != "0")
        {
            SqlParameter[] parmaled = { 
                                       new SqlParameter("@LID",ddlCustomer.SelectedValue)
                                      };
            DataTable dtledadd = DU.getdatausingprocedure("USP_ACC_GetLedgerAddressDetails", parmaled);
            if (dtledadd.Rows.Count > 0)
            {
                txtaddress.Text = dtledadd.Rows[0]["LNAME"].ToString() + " " + dtledadd.Rows[0]["ADDRESS1"].ToString() + " " + dtledadd.Rows[0]["ADDRESS2"].ToString() + " " + dtledadd.Rows[0]["ADDRESS3"].ToString() + " " + dtledadd.Rows[0]["PLACE"].ToString() + " " + dtledadd.Rows[0]["STATENAME"].ToString();
            }
        }
        ClearTable();
        BindCustomerPrefence();

    }
    protected void BindService(DropDownList ddl)
    {
        if (ddlFormat.SelectedValue != "0")
        {
            string strsql = "SELECT SERVICEID, SERVICENAME from IVC_SERVICEMASTER";
            DataTable dtService = DU.getdata(strsql);
            if (dtService.Rows.Count > 0)
            {
                ddl.DataTextField = "SERVICENAME";
                ddl.DataValueField = "SERVICEID";
                ddl.DataSource = dtService;
                ddl.DataBind();

                ListItem li = new ListItem("-select service-", "0");
                li.Selected = true;
                ddl.Items.Insert(0, li);
            }
        }
    }
    protected void ddlservice_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        string ddlid = ddl.ID.ToString().Substring(10);
        int rowno = Convert.ToInt32(ddlid.ToString());
        ((TextBox)tblmain.Rows[rowno].FindControl("txtservicecharge1crtl" + rowno)).Text = string.Empty;
        ((TextBox)tblmain.Rows[rowno].FindControl("txtunitrate2ctrl" + rowno)).Text = string.Empty;
        ((HiddenField)tblmain.Rows[rowno].FindControl("hidServiceCharge" + rowno)).Value = string.Empty;
        ((HiddenField)tblmain.Rows[rowno].FindControl("hidTaxableNonTaxable" + rowno)).Value = string.Empty;
        //((HiddenField)tblmain.Rows[rowno].FindControl("hidTaxableNonTaxable" + rowno)).Value = string.Empty;
        ((HiddenField)tblmain.Rows[rowno].FindControl("hidserviceLid" + rowno)).Value = string.Empty; // Service LedgerId
        SqlParameter[] parmaservice = { 
                                       new SqlParameter("@serviceid",ddl.SelectedValue)
                                      };
        DataTable dtservice = DU.getdatausingprocedure("USP_IVC_GetAllServices_By_ServiceId", parmaservice);
        if (dtservice.Rows.Count > 0)
        {
            if (Convert.ToString(dtservice.Rows[0]["TAXABILITY"]) == "0" && Convert.ToString(dtservice.Rows[0]["SUBCLASS"]) == "1")
            {
                ((HiddenField)tblmain.Rows[rowno].FindControl("hidTaxableNonTaxable" + rowno)).Value = "Taxable";
            }
            else
            {
                ((HiddenField)tblmain.Rows[rowno].FindControl("hidTaxableNonTaxable" + rowno)).Value = "NonTaxable";
            }
            if (Convert.ToString(dtservice.Rows[0]["LEDGERID"]) != string.Empty)
            {
                ((HiddenField)tblmain.Rows[rowno].FindControl("hidserviceLid" + rowno)).Value = dtservice.Rows[0]["LEDGERID"].ToString();
            }
            ((TextBox)tblmain.Rows[rowno].FindControl("txtservicecharge1crtl" + rowno)).Text = String.Format("{0:0.00}", Convert.ToDouble(dtservice.Rows[0]["SERVICECHARGERATE"].ToString()));
            ((HiddenField)tblmain.Rows[rowno].FindControl("hidServiceCharge" + rowno)).Value = String.Format("{0:0.00}", Convert.ToDouble(dtservice.Rows[0]["SERVICECHARGERATE"].ToString()));
        }
    }
    protected void GetDynamicColumn()
    {
        SqlParameter[] parma = { 
                                new SqlParameter("@Formatid",ddlFormat.SelectedValue)
                               };
        DataTable dtcol = DU.getdatausingprocedure("USP_IVC_GetTransactionFormat_EnableDisable", parma);
        if (dtcol.Rows.Count > 0)
        {
            int count = Convert.ToInt32(hcount.Value.ToString());  // Table Control Count
            for (int i = 1; i <= count; i++)
            {
                hidunit.Value = "";
                hidSaleLevel.Value = "";
                hidTaxType.Value = "";
                hidDiscount.Value = "";
                if (dtcol.Rows[0]["TaxLevel"].ToString() == "True")
                {
                    hidSaleLevel.Value = "ItemLevel";

                    if (dtcol.Rows[0]["ItemTaxType"].ToString() == "0")
                    {
                        hidTaxType.Value = "ST"; // Service Tax
                    }
                    else if (dtcol.Rows[0]["ItemTaxType"].ToString() == "1")
                    {
                        hidTaxType.Value = "STLT"; // Service Tax,ServiceTax,Luxuary Tax
                    }
                    else if (dtcol.Rows[0]["ItemTaxType"].ToString() == "2")
                    {
                        hidTaxType.Value = "STVT"; // Service Tax,VAT
                    }

                    if (dtcol.Rows[0]["DiscountLevel"].ToString() == "0")   // Apply Discount
                    {
                        hidDiscount.Value = "None";
                    }
                    else if (dtcol.Rows[0]["DiscountLevel"].ToString() == "2")
                    {
                        hidDiscount.Value = "ItemLevel";
                    }

                }
                else
                {
                    hidSaleLevel.Value = "BillLevel";

                    if (dtcol.Rows[0]["DiscountLevel"].ToString() == "0")   // Apply Discount
                    {
                        hidDiscount.Value = "None";
                    }
                    else if (dtcol.Rows[0]["DiscountLevel"].ToString() == "1")
                    {
                        hidDiscount.Value = "BillLevel";
                    }
                    else if (dtcol.Rows[0]["DiscountLevel"].ToString() == "2")
                    {
                        hidDiscount.Value = "ItemLevel";
                    }
                }
                if (dtcol.Rows[0]["DiscountLevel"].ToString() == "1" && dtcol.Rows[0]["TaxLevel"].ToString() == "True")
                {
                    hidDiscount.Value = "BillLevel";
                }

                if (dtcol.Rows[0]["Adddesc"].ToString() == "True")
                {
                    tddescription.Style.Add("display", "table-cell");
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tddescriptionctrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblmain.Rows[i].FindControl("txtdescriptionctrl" + i.ToString())).Style.Add("display", "table-cell");
                }
                else
                {
                    tddescription.Style.Add("display", "none");
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tddescriptionctrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblmain.Rows[i].FindControl("txtdescriptionctrl" + i.ToString())).Style.Add("display", "none");
                }
                if (dtcol.Rows[0]["Yearreference"].ToString() == "True")
                {
                    tdyearref.Style.Add("display", "table-cell");
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdyearrefctrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblmain.Rows[i].FindControl("txtyearrefctrl" + i.ToString())).Style.Add("display", "table-cell");
                }
                else
                {
                    tdyearref.Style.Add("display", "none");
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdyearrefctrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblmain.Rows[i].FindControl("txtyearrefctrl" + i.ToString())).Style.Add("display", "none");
                }
                if (dtcol.Rows[0]["uom"].ToString() == "True")
                {
                    tduom.Style.Add("display", "table-cell");
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tduomctrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblmain.Rows[i].FindControl("txtuomctrl" + i.ToString())).Style.Add("display", "table-cell");
                }
                else
                {
                    tduom.Style.Add("display", "none");
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tduomctrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblmain.Rows[i].FindControl("txtuomctrl" + i.ToString())).Style.Add("display", "none");
                }
                if (dtcol.Rows[0]["unit2"].ToString() == "True")
                {
                    tdunit2.Style.Add("display", "table-cell");
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdunit2ctrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblmain.Rows[i].FindControl("txtunit2ctrl" + i.ToString())).Style.Add("display", "table-cell");

                    hidunit.Value = "u2";
                }
                else
                {
                    tdunit2.Style.Add("display", "none");
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdunit2ctrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblmain.Rows[i].FindControl("txtunit2ctrl" + i.ToString())).Style.Add("display", "none");
                    hidunit.Value = "";
                }

                if (dtcol.Rows[0]["unit3"].ToString() == "True")
                {
                    tdunit3.Style.Add("display", "table-cell");
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdunit3ctrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblmain.Rows[i].FindControl("txtunit3ctrl" + i.ToString())).Style.Add("display", "table-cell");
                    if (hidunit.Value == "u2")
                    {
                        hidunit.Value = "u2u3";
                    }
                    else
                    {
                        hidunit.Value = "u3";
                    }
                }
                else
                {
                    tdunit3.Style.Add("display", "none");
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdunit3ctrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblmain.Rows[i].FindControl("txtunit3ctrl" + i.ToString())).Style.Add("display", "none");
                    if (hidunit.Value != "u2")
                    {
                        hidunit.Value = "";
                    }
                }
                if (dtcol.Rows[0]["unit4"].ToString() == "True")
                {
                    tdunit4.Style.Add("display", "table-cell");
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdunit4ctrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblmain.Rows[i].FindControl("txtunit4ctrl" + i.ToString())).Style.Add("display", "table-cell");
                    if (hidunit.Value == "u2u3")
                    {
                        hidunit.Value = "u2u3u4";
                    }
                    else if (hidunit.Value == "u2")
                    {
                        hidunit.Value = "u2u4";
                    }
                    else if (hidunit.Value == "u3")
                    {
                        hidunit.Value = "u3u4";
                    }
                    else
                    {
                        hidunit.Value = "u4";
                    }
                }
                else
                {
                    tdunit4.Style.Add("display", "none");
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdunit4ctrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblmain.Rows[i].FindControl("txtunit4ctrl" + i.ToString())).Style.Add("display", "none");
                    if (hidunit.Value != "u2u3" && hidunit.Value != "u2" && hidunit.Value != "u3")
                    {
                        hidunit.Value = "";
                    }
                }
                hidunitrate.Value = "";
                if (dtcol.Rows[0]["unitrate"].ToString() == "True")
                {
                    tdunitrate1.Style.Add("display", "table-cell");
                    tdunitrate2.Style.Add("display", "table-cell");

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdunitrate1ctrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdunitrate2ctrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblmain.Rows[i].FindControl("txtunitrate1ctrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblmain.Rows[i].FindControl("txtunitrate2ctrl" + i.ToString())).Style.Add("display", "table-cell");
                    hidunitrate.Value = "yes";
                }
                else
                {

                    tdunitrate1.Style.Add("display", "none");
                    tdunitrate2.Style.Add("display", "none");

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdunitrate1ctrl" + i.ToString())).Style.Add("display", "none");
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdunitrate2ctrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblmain.Rows[i].FindControl("txtunitrate1ctrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblmain.Rows[i].FindControl("txtunitrate2ctrl" + i.ToString())).Style.Add("display", "none");
                    hidunitrate.Value = "No";

                    if (hidunit.Value.Contains("u2"))
                    {
                        tdunitrate2.Style.Add("display", "table-cell");

                        ((HtmlTableCell)tblmain.Rows[i].FindControl("tdunitrate2ctrl" + i.ToString())).Style.Add("display", "table-cell");
                        ((TextBox)tblmain.Rows[i].FindControl("txtunitrate2ctrl" + i.ToString())).Style.Add("display", "table-cell");
                    }
                    if (hidunit.Value.Contains("u3"))
                    {

                        tdunitrate2.Style.Add("display", "table-cell");

                        ((HtmlTableCell)tblmain.Rows[i].FindControl("tdunitrate2ctrl" + i.ToString())).Style.Add("display", "table-cell");
                        ((TextBox)tblmain.Rows[i].FindControl("txtunitrate2ctrl" + i.ToString())).Style.Add("display", "table-cell");
                    }
                    if (hidunit.Value.Contains("u4"))
                    {

                        tdunitrate2.Style.Add("display", "table-cell");

                        ((HtmlTableCell)tblmain.Rows[i].FindControl("tdunitrate2ctrl" + i.ToString())).Style.Add("display", "table-cell");
                        ((TextBox)tblmain.Rows[i].FindControl("txtunitrate2ctrl" + i.ToString())).Style.Add("display", "table-cell");
                    }
                }


                if (dtcol.Rows[0]["DiscountLevel"].ToString() == "1") // Discount Bill Level
                {

                    tddiscount1.Style.Add("display", "none");
                    tddiscount2.Style.Add("display", "none");


                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tddiscount1ctrl" + i.ToString())).Style.Add("display", "none");
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tddiscount2ctrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblmain.Rows[i].FindControl("txtdiscount1ctrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblmain.Rows[i].FindControl("txtdiscount2ctrl" + i.ToString())).Style.Add("display", "none");

                    tddiscounttax.Style.Add("display", "table-cell");
                    tddiscounttaxper.Style.Add("display", "table-cell");
                    txtdiscounttaxper.Style.Add("display", "table-cell");
                    tddiscounttaxamt.Style.Add("display", "table-cell");
                    txtdiscounttaxamt.Style.Add("display", "table-cell");

                    tddiscountnontax.Style.Add("display", "table-cell");
                    tdnondiscountper.Style.Add("display", "table-cell");
                    txtnondiscounttaxper.Style.Add("display", "table-cell");
                    tdnondiscounttaxamt.Style.Add("display", "table-cell");
                    txtnondiscounttaxamt.Style.Add("display", "table-cell");
                }
                else if (dtcol.Rows[0]["Discountlevel"].ToString() == "2")  // Discount Item Level
                {
                    tddiscount1.Style.Add("display", "table-cell");
                    tddiscount2.Style.Add("display", "table-cell");

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tddiscount1ctrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tddiscount2ctrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblmain.Rows[i].FindControl("txtdiscount1ctrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblmain.Rows[i].FindControl("txtdiscount2ctrl" + i.ToString())).Style.Add("display", "table-cell");

                    tddiscounttax.Style.Add("display", "table-cell");
                    tddiscounttaxper.Style.Add("display", "table-cell");
                    tddiscounttaxamt.Style.Add("display", "table-cell");
                    txtdiscounttaxamt.Style.Add("display", "table-cell");
                    tddiscountnontax.Style.Add("display", "table-cell");
                    tdnondiscountper.Style.Add("display", "table-cell");
                    tdnondiscounttaxamt.Style.Add("display", "table-cell");
                    txtnondiscounttaxamt.Style.Add("display", "table-cell");


                    txtnondiscounttaxper.Style.Add("display", "none");
                    txtdiscounttaxper.Style.Add("display", "none");

                }
                else
                {
                    tddiscount1.Style.Add("display", "none");
                    tddiscount2.Style.Add("display", "none");

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tddiscount1ctrl" + i.ToString())).Style.Add("display", "none");
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tddiscount2ctrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblmain.Rows[i].FindControl("txtdiscount1ctrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblmain.Rows[i].FindControl("txtdiscount2ctrl" + i.ToString())).Style.Add("display", "none");

                    tddiscounttax.Style.Add("display", "none");
                    tddiscounttaxper.Style.Add("display", "none");
                    txtdiscounttaxper.Style.Add("display", "none");
                    tddiscounttaxamt.Style.Add("display", "none");
                    txtdiscounttaxamt.Style.Add("display", "none");

                    tddiscountnontax.Style.Add("display", "none");
                    tdnondiscountper.Style.Add("display", "none");
                    txtnondiscounttaxper.Style.Add("display", "none");
                    tdnondiscounttaxamt.Style.Add("display", "none");
                    txtnondiscounttaxamt.Style.Add("display", "none");

                }
                if (dtcol.Rows[0]["ShowserviceCharge"].ToString() == "True")
                {
                    tdservicecharge1.Style.Add("display", "table-cell");
                    tdservicecharge2.Style.Add("display", "table-cell");
                    tdservicecharge3.Style.Add("display", "table-cell");

                    tdservicechargeamt.Style.Add("display", "table-cell");
                    tdservicechargeamttext.Style.Add("display", "table-cell");
                    txtservicechargeamt.Style.Add("display", "table-cell");

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdservicecharge1crtl" + i.ToString())).Style.Add("display", "table-cell");
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdservicecharge2crtl" + i.ToString())).Style.Add("display", "table-cell");
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdservicecharge3crtl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblmain.Rows[i].FindControl("txtservicecharge1crtl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblmain.Rows[i].FindControl("txtservicecharge2crtl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblmain.Rows[i].FindControl("txtservicecharge3crtl" + i.ToString())).Style.Add("display", "table-cell");
                    hidsercharge.Value = "yes";
                }
                else
                {
                    tdservicecharge1.Style.Add("display", "none");
                    tdservicecharge2.Style.Add("display", "none");
                    tdservicecharge3.Style.Add("display", "none");

                    tdservicechargeamt.Style.Add("display", "none");
                    tdservicechargeamttext.Style.Add("display", "none");
                    txtservicechargeamt.Style.Add("display", "none");

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdservicecharge1crtl" + i.ToString())).Style.Add("display", "none");
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdservicecharge2crtl" + i.ToString())).Style.Add("display", "none");
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdservicecharge3crtl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblmain.Rows[i].FindControl("txtservicecharge1crtl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblmain.Rows[i].FindControl("txtservicecharge2crtl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblmain.Rows[i].FindControl("txtservicecharge3crtl" + i.ToString())).Style.Add("display", "none");
                    hidsercharge.Value = "no";
                }
                if (Convert.ToBoolean(dtcol.Rows[0]["Taxlevel"].ToString()) == true)
                {
                    tdserviceTax1.Style.Add("display", "table-cell");
                    tdserviceTax2.Style.Add("display", "table-cell");
                    tdserviceTax3.Style.Add("display", "table-cell");

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdserviceTax1crtl" + i.ToString())).Style.Add("display", "table-cell");
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdserviceTax2crtl" + i.ToString())).Style.Add("display", "table-cell");
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdserviceTax3crtl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblmain.Rows[i].FindControl("txtserviceTax1crtl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblmain.Rows[i].FindControl("txtserviceTax2crtl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblmain.Rows[i].FindControl("txtserviceTax3crtl" + i.ToString())).Style.Add("display", "table-cell");

                    if (dtcol.Rows[0]["ItemTaxType"].ToString() == "1")
                    {

                        tdserviceTax4.Style.Add("display", "table-cell");


                        ((HtmlTableCell)tblmain.Rows[i].FindControl("tdserviceTax4crtl" + i.ToString())).Style.Add("display", "table-cell");
                        ((TextBox)tblmain.Rows[i].FindControl("txtserviceTax4crtl" + i.ToString())).Style.Add("display", "table-cell");
                    }
                    else if (dtcol.Rows[0]["ItemTaxType"].ToString() == "2")
                    {
                        tdserviceTax5.Style.Add("display", "table-cell");


                        ((HtmlTableCell)tblmain.Rows[i].FindControl("tdserviceTax5crtl" + i.ToString())).Style.Add("display", "table-cell");
                        ((TextBox)tblmain.Rows[i].FindControl("txtserviceTax5crtl" + i.ToString())).Style.Add("display", "table-cell");
                    }
                }
                else
                {
                    tdserviceTax1.Style.Add("display", "none");
                    tdserviceTax2.Style.Add("display", "none");
                    tdserviceTax3.Style.Add("display", "none");


                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdserviceTax1crtl" + i.ToString())).Style.Add("display", "none");
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdserviceTax2crtl" + i.ToString())).Style.Add("display", "none");
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdserviceTax3crtl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblmain.Rows[i].FindControl("txtserviceTax1crtl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblmain.Rows[i].FindControl("txtserviceTax2crtl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblmain.Rows[i].FindControl("txtserviceTax3crtl" + i.ToString())).Style.Add("display", "none");

                    tdserviceTax4.Style.Add("display", "none");

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdserviceTax4crtl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblmain.Rows[i].FindControl("txtserviceTax4crtl" + i.ToString())).Style.Add("display", "none");

                    tdserviceTax5.Style.Add("display", "none");

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdserviceTax5crtl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblmain.Rows[i].FindControl("txtserviceTax5crtl" + i.ToString())).Style.Add("display", "none");
                }
                if (dtcol.Rows[0]["Taxinclusive"].ToString() == "True" || dtcol.Rows[0]["ItemTaxType"].ToString() == "0")
                {
                    tdTaxincGrandTotal.Style.Add("display", "table-cell");


                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdTaxincGrandTotalctrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblmain.Rows[i].FindControl("txtTaxincGrandTotalctrl" + i.ToString())).Style.Add("display", "table-cell");
                    hidGrandTotal.Value = "yes";
                    ((TextBox)tblmain.Rows[i].FindControl("txtgrossamtctrl" + i.ToString())).Attributes.Add("onKeyDown", "return EnabledTabKey(this.id, event);");
                }
                else
                {
                    tdTaxincGrandTotal.Style.Add("display", "none");
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdTaxincGrandTotalctrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblmain.Rows[i].FindControl("txtTaxincGrandTotalctrl" + i.ToString())).Style.Add("display", "none");
                    hidGrandTotal.Value = "no";
                    if (dtcol.Rows[0]["unitrate"].ToString() == "True")
                    {
                    }
                    else
                    {
                        if (hidunit.Value != string.Empty)
                        {
                            tdTaxincGrandTotal.Style.Add("display", "table-cell");
                            ((HtmlTableCell)tblmain.Rows[i].FindControl("tdTaxincGrandTotalctrl" + i.ToString())).Style.Add("display", "table-cell");
                            ((TextBox)tblmain.Rows[i].FindControl("txtTaxincGrandTotalctrl" + i.ToString())).Style.Add("display", "table-cell");
                            ((TextBox)tblmain.Rows[i].FindControl("txtTaxincGrandTotalctrl" + i.ToString())).Attributes.Add("onKeyDown", "return EnabledTabKey(this.id, event);");
                            ((TextBox)tblmain.Rows[i].FindControl("txtTaxincGrandTotalctrl" + i.ToString())).CssClass = "DisableInputNumber";
                        }
                        else
                        {
                            tdTaxincGrandTotal.Style.Add("display", "table-cell");
                            ((HtmlTableCell)tblmain.Rows[i].FindControl("tdTaxincGrandTotalctrl" + i.ToString())).Style.Add("display", "table-cell");
                            ((TextBox)tblmain.Rows[i].FindControl("txtTaxincGrandTotalctrl" + i.ToString())).Style.Add("display", "table-cell");
                            ((TextBox)tblmain.Rows[i].FindControl("txtTaxincGrandTotalctrl" + i.ToString())).Attributes.Add("onKeyDown", "return EnabledTabKey(this.id, event);");
                            ((TextBox)tblmain.Rows[i].FindControl("txtTaxincGrandTotalctrl" + i.ToString())).CssClass = "DisableInputNumber";
                        }
                    }
                }

                if (dtcol.Rows[0]["NonTaxable"].ToString() == "True")
                {
                    tdnontaxableamt.Style.Add("display", "table-cell");
                    tdnontaxableamttext.Style.Add("display", "table-cell");
                    txtnontaxableamount.Style.Add("display", "table-cell");
                }
                else
                {
                    tdnontaxableamt.Style.Add("display", "none");
                    tdnontaxableamttext.Style.Add("display", "none");
                    txtnontaxableamount.Style.Add("display", "none");
                }
            }

        }
    }
    protected void AddMore_Click(Object sender, EventArgs e)
    {
        ADDMoreRow();
        GetDynamicColumn();
    }
    protected void ADDMoreRow()
    {
        counter++;
        listcounter.Add(counter.ToString());

        HtmlTableRow row = new HtmlTableRow();

        HtmlTableCell cell0 = new HtmlTableCell();
        cell0.Align = "center";
        cell0.Width = "20px";
        CheckBox chkDelete = new CheckBox();
        chkDelete.ID = "chkDelete" + counter.ToString();
        chkDelete.Width = Unit.Pixel(20);
        chkDelete.Attributes.Add("onclick", "checkAlLChecked();CalculateGrandTotal(this.id);");
        cell0.Controls.Add(chkDelete);
        LstChkDelete.Add(chkDelete.ID);
        HiddenField hidChildID = new HiddenField();
        hidChildID.ID = "hidChildID" + counter.ToString();
        hidChildID.Value = "0";
        cell0.Controls.Add(hidChildID);
        LsthidChildID.Add(hidChildID.ID);

        HtmlTableCell cell1 = new HtmlTableCell();
        cell1.ID = "tdservicectrl" + counter.ToString();
        cell1.Align = "Center";
        cell1.Width = "150px";
        listtdservicectrl.Add(cell1.ID.ToString());

        DropDownList ddlservice = new DropDownList();
        ddlservice.ID = "ddlservice" + counter.ToString();
        ddlservice.CssClass = "dropdown";
        ddlservice.Width = Unit.Pixel(150);
        ddlservice.AutoPostBack = true;
        BindService(ddlservice);
        ddlservice.Attributes.Add("onchange", "return ValidateRequired(this.id);");
        ddlservice.SelectedIndexChanged += new EventHandler(ddlservice_SelectedIndexChanged);

        listddlservice.Add(ddlservice.ID.ToString());

        HiddenField hidTaxableNonTaxable = new HiddenField();
        hidTaxableNonTaxable.ID = "hidTaxableNonTaxable" + counter.ToString();
        listhidTaxableNonTaxable.Add(hidTaxableNonTaxable.ID.ToString());

        HiddenField hidserviceLid = new HiddenField();
        hidserviceLid.ID = "hidserviceLid" + counter.ToString();
        listhidserviceLid.Add(hidserviceLid.ID.ToString());

        HtmlTableCell cell2 = new HtmlTableCell();
        cell2.ID = "tddescriptionctrl" + counter.ToString();
        cell2.Align = "Center";
        cell2.Width = "120px";
        cell2.Style.Add("display", "none");
        listtddescriptionctrl.Add(cell2.ID.ToString());

        TextBox txtdescriptionctrl = new TextBox();
        txtdescriptionctrl.ID = "txtdescriptionctrl" + counter.ToString();
        txtdescriptionctrl.Style.Add("display", "none");
        txtdescriptionctrl.Width = Unit.Pixel(120);
        txtdescriptionctrl.MaxLength = 248;
        txtdescriptionctrl.CssClass = "input";
        listtxtdescriptionctrl.Add(txtdescriptionctrl.ID.ToString());

        HtmlTableCell cell3 = new HtmlTableCell();
        cell3.ID = "tdyearrefctrl" + counter.ToString();
        cell3.Align = "Center";
        cell3.Width = "70px";
        cell3.Style.Add("display", "none");
        listtdyearrefctrl.Add(cell3.ID.ToString());

        TextBox txtyearrefctrl = new TextBox();
        txtyearrefctrl.ID = "txtyearrefctrl" + counter.ToString();
        txtyearrefctrl.Width = Unit.Pixel(70);
        txtyearrefctrl.MaxLength = 48;
        txtyearrefctrl.Style.Add("display", "none");
        txtyearrefctrl.CssClass = "input";
        listtxtyearrefctrl.Add(txtyearrefctrl.ID.ToString());

        HtmlTableCell cell4 = new HtmlTableCell();
        cell4.ID = "tduomctrl" + counter.ToString();
        cell4.Align = "Center";
        cell4.Width = "70px";
        cell4.Style.Add("display", "none");
        listtduomctrl.Add(cell4.ID.ToString());

        TextBox txtuomctrl = new TextBox();
        txtuomctrl.ID = "txtuomctrl" + counter.ToString();
        txtuomctrl.Width = Unit.Pixel(70);
        txtuomctrl.Style.Add("display", "none");
        txtuomctrl.MaxLength = 25;
        txtuomctrl.CssClass = "input";
        listtxtuomctrl.Add(txtuomctrl.ID.ToString());

        HtmlTableCell cell5 = new HtmlTableCell();
        cell5.ID = "tdunit2ctrl" + counter.ToString();
        cell5.Align = "Center";
        cell5.Width = "70px";
        cell5.Style.Add("display", "none");
        listtdunit2ctrl.Add(cell5.ID.ToString());

        TextBox txtunit2ctrl = new TextBox();
        txtunit2ctrl.ID = "txtunit2ctrl" + counter.ToString();
        txtunit2ctrl.Width = Unit.Pixel(70);
        txtunit2ctrl.Style.Add("display", "none");
        txtunit2ctrl.MaxLength = 10;
        txtunit2ctrl.CssClass = "inputnumber";
        txtunit2ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");
        listtxtunit2ctrl.Add(txtunit2ctrl.ID.ToString());

        HtmlTableCell cell6 = new HtmlTableCell();
        cell6.ID = "tdunit3ctrl" + counter.ToString();
        cell6.Align = "Center";
        cell6.Width = "70px";
        cell6.Style.Add("display", "none");
        listtdunit3ctrl.Add(cell6.ID.ToString());

        TextBox txtunit3ctrl = new TextBox();
        txtunit3ctrl.ID = "txtunit3ctrl" + counter.ToString();
        txtunit3ctrl.Width = Unit.Pixel(70);
        txtunit3ctrl.MaxLength = 10;
        txtunit3ctrl.Style.Add("display", "none");
        txtunit3ctrl.CssClass = "inputnumber";
        txtunit3ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");
        listtxtunit3ctrl.Add(txtunit3ctrl.ID.ToString());


        HtmlTableCell cell7 = new HtmlTableCell();
        cell7.ID = "tdunit4ctrl" + counter.ToString();
        cell7.Align = "Center";
        cell7.Width = "70px";
        cell7.Style.Add("display", "none");
        listtdunit4ctrl.Add(cell7.ID.ToString());

        TextBox txtunit4ctrl = new TextBox();
        txtunit4ctrl.ID = "txtunit4ctrl" + counter.ToString();
        txtunit4ctrl.Width = Unit.Pixel(70);
        txtunit4ctrl.MaxLength = 10;
        txtunit4ctrl.Style.Add("display", "none");
        txtunit4ctrl.CssClass = "inputnumber";
        txtunit4ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");
        listtxtunit4ctrl.Add(txtunit4ctrl.ID.ToString());

        HtmlTableCell cell8 = new HtmlTableCell();
        cell8.ID = "tdunitrate1ctrl" + counter.ToString();
        cell8.Align = "Center";
        cell8.Width = "70px";
        cell8.Style.Add("display", "none");
        listtdunitrate1ctrl.Add(cell8.ID.ToString());

        TextBox txtunitrate1ctrl = new TextBox();
        txtunitrate1ctrl.ID = "txtunitrate1ctrl" + counter.ToString();
        txtunitrate1ctrl.Width = Unit.Pixel(70);
        txtunitrate1ctrl.MaxLength = 10;
        txtunitrate1ctrl.Style.Add("display", "none");
        txtunitrate1ctrl.CssClass = "inputnumber";
        txtunitrate1ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");
        txtunitrate1ctrl.Attributes.Add("onBlur", "return CalculateGrandTotal(this.id);");
        listtxtunitrate1ctrl.Add(txtunitrate1ctrl.ID.ToString());

        HtmlTableCell cell9 = new HtmlTableCell();
        cell9.ID = "tdunitrate2ctrl" + counter.ToString();
        cell9.Align = "Center";
        cell9.Width = "70px";
        cell9.Style.Add("display", "none");
        listtdunitrate2ctrl.Add(cell9.ID.ToString());

        TextBox txtunitrate2ctrl = new TextBox();
        txtunitrate2ctrl.ID = "txtunitrate2ctrl" + counter.ToString();
        txtunitrate2ctrl.Width = Unit.Pixel(70);
        //txtunitrate2ctrl.Style.Add("display","none");
        txtunitrate2ctrl.MaxLength = 10;
        txtunitrate2ctrl.CssClass = "inputnumber";
        txtunitrate2ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");
        txtunitrate2ctrl.Attributes.Add("onBlur", "return CalculateGrandTotal(this.id);");
        listtxtunitrate2ctrl.Add(txtunitrate2ctrl.ID.ToString());

        HtmlTableCell cell10 = new HtmlTableCell();
        cell10.ID = "tdgrossamtctrl" + counter.ToString();
        cell10.Align = "Center";
        cell10.Width = "120px";
        //cell10.Style.Add("display","none");
        listtdgrossamtctrl.Add(cell10.ID.ToString());

        TextBox txtgrossamtctrl = new TextBox();
        txtgrossamtctrl.ID = "txtgrossamtctrl" + counter.ToString();
        txtgrossamtctrl.Width = Unit.Pixel(120);
        //txtgrossamtctrl.Style.Add("display","none");
        txtgrossamtctrl.CssClass = "inputnumber";
        txtgrossamtctrl.Attributes.Add("OnKeyDown", "return isNumberKey(this.id,event);");
        txtgrossamtctrl.Attributes.Add("onBlur", "return CalculateGrandTotal(this.id);");
        listtxtgrossamtctrl.Add(txtgrossamtctrl.ID.ToString());

        HiddenField hidServiceCharge = new HiddenField();
        hidServiceCharge.ID = "hidServiceCharge" + counter.ToString();
        listhidServiceCharge.Add(hidServiceCharge.ID.ToString());

        HtmlTableCell cell11 = new HtmlTableCell();
        cell11.ID = "tddiscount1ctrl" + counter.ToString();
        cell11.Align = "Center";
        cell11.Width = "80px";
        cell11.Style.Add("display", "none");
        listtddiscount1ctrl.Add(cell11.ID.ToString());

        TextBox txtdiscount1ctrl = new TextBox();
        txtdiscount1ctrl.ID = "txtdiscount1ctrl" + counter.ToString();
        txtdiscount1ctrl.Width = Unit.Pixel(80);
        txtdiscount1ctrl.Style.Add("display", "none");
        txtdiscount1ctrl.CssClass = "inputnumber";
        txtdiscount1ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");
        txtdiscount1ctrl.Attributes.Add("onBlur", "return CalculateGrandTotal(this.id);");
        listtxtdiscount1ctrl.Add(txtdiscount1ctrl.ID.ToString());

        HtmlTableCell cell12 = new HtmlTableCell();
        cell12.ID = "tddiscount2ctrl" + counter.ToString();
        cell12.Align = "Center";
        cell12.Width = "120px";
        cell12.Style.Add("display", "none");
        listtddiscount2ctrl.Add(cell12.ID.ToString());

        TextBox txtdiscount2ctrl = new TextBox();
        txtdiscount2ctrl.ID = "txtdiscount2ctrl" + counter.ToString();
        txtdiscount2ctrl.Width = Unit.Pixel(120);
        txtdiscount2ctrl.Style.Add("display", "none");
        txtdiscount2ctrl.CssClass = "DisableInputNumber";
        txtdiscount2ctrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");
        listtxtdiscount2ctrl.Add(txtdiscount2ctrl.ID.ToString());

        HtmlTableCell cell13 = new HtmlTableCell();
        cell13.ID = "tdservicecharge1crtl" + counter.ToString();
        cell13.Align = "Center";
        cell13.Width = "120px";
        cell13.Style.Add("display", "none");
        listtdservicecharge1crtl.Add(cell13.ID.ToString());

        TextBox txtservicecharge1crtl = new TextBox();
        txtservicecharge1crtl.ID = "txtservicecharge1crtl" + counter.ToString();
        txtservicecharge1crtl.Width = Unit.Pixel(120);
        txtservicecharge1crtl.Style.Add("display", "none");
        txtservicecharge1crtl.CssClass = "inputnumber";
        txtservicecharge1crtl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");
        txtservicecharge1crtl.Attributes.Add("onBlur", "return CalculateGrandTotal(this.id);");
        listtxtservicecharge1crtl.Add(txtservicecharge1crtl.ID.ToString());

        HtmlTableCell cell14 = new HtmlTableCell();
        cell14.ID = "tdservicecharge2crtl" + counter.ToString();
        cell14.Align = "Center";
        cell14.Width = "100px";
        cell14.Style.Add("display", "none");
        listtdservicecharge2crtl.Add(cell14.ID.ToString());

        TextBox txtservicecharge2crtl = new TextBox();
        txtservicecharge2crtl.ID = "txtservicecharge2crtl" + counter.ToString();
        txtservicecharge2crtl.Width = Unit.Pixel(100);
        txtservicecharge2crtl.Style.Add("display", "none");
        txtservicecharge2crtl.CssClass = "DisableInputNumber";
        txtservicecharge2crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");
        listtxtservicecharge2crtl.Add(txtservicecharge2crtl.ID.ToString());

        HtmlTableCell cell15 = new HtmlTableCell();
        cell15.ID = "tdservicecharge3crtl" + counter.ToString();
        cell15.Align = "Center";
        cell15.Width = "100px";
        cell15.Style.Add("display", "none");
        listtdservicecharge3crtl.Add(cell15.ID.ToString());

        TextBox txtservicecharge3crtl = new TextBox();
        txtservicecharge3crtl.ID = "txtservicecharge3crtl" + counter.ToString();
        txtservicecharge3crtl.Width = Unit.Pixel(100);
        txtservicecharge3crtl.Style.Add("display", "none");
        txtservicecharge3crtl.CssClass = "DisableInputNumber";
        txtservicecharge3crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");
        listtxtservicecharge3crtl.Add(txtservicecharge3crtl.ID.ToString());

        HtmlTableCell cell16 = new HtmlTableCell();
        cell16.ID = "tdserviceTax1crtl" + counter.ToString();
        cell16.Align = "Center";
        cell16.Width = "100px";
        cell16.Style.Add("display", "none");
        listtdserviceTax1crtl.Add(cell16.ID.ToString());

        TextBox txtserviceTax1crtl = new TextBox();
        txtserviceTax1crtl.ID = "txtserviceTax1crtl" + counter.ToString();
        txtserviceTax1crtl.Width = Unit.Pixel(100);
        txtserviceTax1crtl.Style.Add("display", "none");
        txtserviceTax1crtl.CssClass = "DisableInputNumber";
        txtserviceTax1crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");
        listtxtserviceTax1crtl.Add(txtserviceTax1crtl.ID.ToString());

        HtmlTableCell cell17 = new HtmlTableCell();
        cell17.ID = "tdserviceTax2crtl" + counter.ToString();
        cell17.Align = "Center";
        cell17.Width = "70px";
        cell17.Style.Add("display", "none");
        listtdserviceTax2crtl.Add(cell17.ID.ToString());

        TextBox txtserviceTax2crtl = new TextBox();
        txtserviceTax2crtl.ID = "txtserviceTax2crtl" + counter.ToString();
        txtserviceTax2crtl.Width = Unit.Pixel(70);
        txtserviceTax2crtl.Style.Add("display", "none");
        txtserviceTax2crtl.CssClass = "DisableInputNumber";
        txtserviceTax2crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");
        listtxtserviceTax2crtl.Add(txtserviceTax2crtl.ID.ToString());

        HtmlTableCell cell18 = new HtmlTableCell();
        cell18.ID = "tdserviceTax3crtl" + counter.ToString();
        cell18.Align = "Center";
        cell18.Width = "70px";
        cell18.Style.Add("display", "none");
        listtdserviceTax3crtl.Add(cell18.ID.ToString());

        TextBox txtserviceTax3crtl = new TextBox();
        txtserviceTax3crtl.ID = "txtserviceTax3crtl" + counter.ToString();
        txtserviceTax3crtl.Width = Unit.Pixel(70);
        txtserviceTax3crtl.Style.Add("display", "none");
        txtserviceTax3crtl.CssClass = "DisableInputNumber";
        txtserviceTax3crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");
        listtxtserviceTax3crtl.Add(txtserviceTax3crtl.ID.ToString());

        HtmlTableCell cell19 = new HtmlTableCell();
        cell19.ID = "tdserviceTax4crtl" + counter.ToString();
        cell19.Align = "Center";
        cell19.Width = "70px";
        cell19.Style.Add("display", "none");
        listtdserviceTax4crtl.Add(cell19.ID.ToString());

        TextBox txtserviceTax4crtl = new TextBox();
        txtserviceTax4crtl.ID = "txtserviceTax4crtl" + counter.ToString();
        txtserviceTax4crtl.Width = Unit.Pixel(70);
        txtserviceTax4crtl.Style.Add("display", "none");
        txtserviceTax4crtl.CssClass = "DisableInputNumber";
        txtserviceTax4crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");
        listtxtserviceTax4crtl.Add(txtserviceTax4crtl.ID.ToString());

        HtmlTableCell cell20 = new HtmlTableCell();
        cell20.ID = "tdserviceTax5crtl" + counter.ToString();
        cell20.Align = "Center";
        cell20.Width = "70px";
        cell20.Style.Add("display", "none");
        listtdserviceTax5crtl.Add(cell20.ID.ToString());

        TextBox txtserviceTax5crtl = new TextBox();
        txtserviceTax5crtl.ID = "txtserviceTax5crtl" + counter.ToString();
        txtserviceTax5crtl.Width = Unit.Pixel(70);
        txtserviceTax5crtl.Style.Add("display", "none");
        txtserviceTax5crtl.CssClass = "DisableInputNumber";
        txtserviceTax5crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");
        listtxtserviceTax5crtl.Add(txtserviceTax5crtl.ID.ToString());


        HtmlTableCell cell21 = new HtmlTableCell();
        cell21.ID = "tdTaxincGrandTotalctrl" + counter.ToString();
        cell21.Align = "Center";
        cell21.Width = "120px";
        cell21.Style.Add("display", "none");
        listtdTaxincGrandTotalctrl.Add(cell21.ID.ToString());

        TextBox txtTaxincGrandTotalctrl = new TextBox();
        txtTaxincGrandTotalctrl.ID = "txtTaxincGrandTotalctrl" + counter.ToString();
        txtTaxincGrandTotalctrl.Width = Unit.Pixel(120);
        txtTaxincGrandTotalctrl.Style.Add("display", "none");
        txtTaxincGrandTotalctrl.CssClass = "DisableInputNumber";
        txtTaxincGrandTotalctrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");
        txtTaxincGrandTotalctrl.Attributes.Add("onBlur", "return CalculateGrandTotal(this.id);");
        listtxtTaxincGrandTotalctrl.Add(txtTaxincGrandTotalctrl.ID.ToString());

        cell1.Controls.Add(ddlservice);
        cell1.Controls.Add(hidTaxableNonTaxable);
        cell1.Controls.Add(hidserviceLid);
        cell2.Controls.Add(txtdescriptionctrl);
        cell3.Controls.Add(txtyearrefctrl);
        cell4.Controls.Add(txtuomctrl);
        cell5.Controls.Add(txtunit2ctrl);
        cell6.Controls.Add(txtunit3ctrl);
        cell7.Controls.Add(txtunit4ctrl);
        cell8.Controls.Add(txtunitrate1ctrl);
        cell9.Controls.Add(txtunitrate2ctrl);
        cell10.Controls.Add(txtgrossamtctrl);
        cell10.Controls.Add(hidServiceCharge);
        cell11.Controls.Add(txtdiscount1ctrl);
        cell12.Controls.Add(txtdiscount2ctrl);
        cell13.Controls.Add(txtservicecharge1crtl);
        cell14.Controls.Add(txtservicecharge2crtl);
        cell15.Controls.Add(txtservicecharge3crtl);
        cell16.Controls.Add(txtserviceTax1crtl);
        cell17.Controls.Add(txtserviceTax2crtl);
        cell18.Controls.Add(txtserviceTax3crtl);
        cell19.Controls.Add(txtserviceTax4crtl);
        cell20.Controls.Add(txtserviceTax5crtl);
        cell21.Controls.Add(txtTaxincGrandTotalctrl);

        row.Cells.Add(cell0);
        row.Cells.Add(cell1);
        row.Cells.Add(cell2);
        row.Cells.Add(cell3);
        row.Cells.Add(cell4);
        row.Cells.Add(cell5);
        row.Cells.Add(cell6);
        row.Cells.Add(cell7);
        row.Cells.Add(cell8);
        row.Cells.Add(cell9);
        row.Cells.Add(cell10);
        row.Cells.Add(cell11);
        row.Cells.Add(cell12);
        row.Cells.Add(cell13);
        row.Cells.Add(cell14);
        row.Cells.Add(cell15);
        row.Cells.Add(cell16);
        row.Cells.Add(cell17);
        row.Cells.Add(cell18);
        row.Cells.Add(cell19);
        row.Cells.Add(cell20);
        row.Cells.Add(cell21);

        tblmain.Rows.Add(row);

        hcount.Value = counter.ToString();

        ViewState["listcounter"] = listcounter;

        ViewState["listtdservicectrl"] = listtdservicectrl;
        ViewState["listddlservice"] = listddlservice;
        ViewState["listhidTaxableNonTaxable"] = listhidTaxableNonTaxable;
        ViewState["listhidserviceLid"] = listhidserviceLid;

        ViewState["listtddescriptionctrl"] = listtddescriptionctrl;
        ViewState["listtxtdescriptionctrl"] = listtxtdescriptionctrl;

        ViewState["listtdyearrefctrl"] = listtdyearrefctrl;
        ViewState["listtxtyearrefctrl"] = listtxtyearrefctrl;

        ViewState["listtduomctrl"] = listtduomctrl;
        ViewState["listtxtuomctrl"] = listtxtuomctrl;

        ViewState["listtdunit2ctrl"] = listtdunit2ctrl;
        ViewState["listtxtunit2ctrl"] = listtxtunit2ctrl;
        ViewState["listtdunit3ctrl"] = listtdunit3ctrl;
        ViewState["listtxtunit3ctrl"] = listtxtunit3ctrl;
        ViewState["listtdunit4ctrl"] = listtdunit4ctrl;
        ViewState["listtxtunit4ctrl"] = listtxtunit4ctrl;

        ViewState["listtdunitrate1ctrl"] = listtdunitrate1ctrl;
        ViewState["listtxtunitrate1ctrl"] = listtxtunitrate1ctrl;
        ViewState["listtdunitrate2ctrl"] = listtdunitrate2ctrl;
        ViewState["listtxtunitrate2ctrl"] = listtxtunitrate2ctrl;

        ViewState["listtdgrossamtctrl"] = listtdgrossamtctrl;
        ViewState["listtxtgrossamtctrl"] = listtxtgrossamtctrl;

        ViewState["listtddiscount1ctrl"] = listtddiscount1ctrl;
        ViewState["listtxtdiscount1ctrl"] = listtxtdiscount1ctrl;

        ViewState["listtddiscount2ctrl"] = listtddiscount2ctrl;
        ViewState["listtxtdiscount2ctrl"] = listtxtdiscount2ctrl;

        ViewState["listtdservicecharge1crtl"] = listtdservicecharge1crtl;
        ViewState["listhidServiceCharge"] = listhidServiceCharge;
        ViewState["listtxtservicecharge1crtl"] = listtxtservicecharge1crtl;
        ViewState["listtdservicecharge2crtl"] = listtdservicecharge2crtl;
        ViewState["listtxtservicecharge2crtl"] = listtxtservicecharge2crtl;
        ViewState["listtdservicecharge3crtl"] = listtdservicecharge3crtl;
        ViewState["listtxtservicecharge3crtl"] = listtxtservicecharge3crtl;

        ViewState["listtdserviceTax1crtl"] = listtdserviceTax1crtl;
        ViewState["listtxtserviceTax1crtl"] = listtxtserviceTax1crtl;
        ViewState["listtdserviceTax2crtl"] = listtdserviceTax2crtl;
        ViewState["listtxtserviceTax2crtl"] = listtxtserviceTax2crtl;
        ViewState["listtdserviceTax3crtl"] = listtdserviceTax3crtl;
        ViewState["listtxtserviceTax3crtl"] = listtxtserviceTax3crtl;
        ViewState["listtdserviceTax4crtl"] = listtdserviceTax4crtl;
        ViewState["listtxtserviceTax4crtl"] = listtxtserviceTax4crtl;
        ViewState["listtdserviceTax5crtl"] = listtdserviceTax5crtl;
        ViewState["listtxtserviceTax5crtl"] = listtxtserviceTax5crtl;

        ViewState["listtdTaxincGrandTotalctrl"] = listtdTaxincGrandTotalctrl;
        ViewState["listtxtTaxincGrandTotalctrl"] = listtxtTaxincGrandTotalctrl;
        ViewState["LstChkDelete"] = LstChkDelete;
        ViewState["LsthidChildID"] = LsthidChildID;
    }
    protected void OverrideAddMore()
    {

        listcounter = (List<string>)ViewState["listcounter"];

        listtdservicectrl = (List<string>)ViewState["listtdservicectrl"];
        listddlservice = (List<string>)ViewState["listddlservice"];

        listhidTaxableNonTaxable = (List<string>)ViewState["listhidTaxableNonTaxable"];
        listhidserviceLid = (List<string>)ViewState["listhidserviceLid"];

        listtddescriptionctrl = (List<string>)ViewState["listtddescriptionctrl"];
        listtxtdescriptionctrl = (List<string>)ViewState["listtxtdescriptionctrl"];

        listtdyearrefctrl = (List<string>)ViewState["listtdyearrefctrl"];
        listtxtyearrefctrl = (List<string>)ViewState["listtxtyearrefctrl"];

        listtduomctrl = (List<string>)ViewState["listtduomctrl"];
        listtxtuomctrl = (List<string>)ViewState["listtxtuomctrl"];

        listtdunit2ctrl = (List<string>)ViewState["listtdunit2ctrl"];
        listtxtunit2ctrl = (List<string>)ViewState["listtxtunit2ctrl"];
        listtdunit3ctrl = (List<string>)ViewState["listtdunit3ctrl"];
        listtxtunit3ctrl = (List<string>)ViewState["listtxtunit3ctrl"];
        listtdunit4ctrl = (List<string>)ViewState["listtdunit4ctrl"];
        listtxtunit4ctrl = (List<string>)ViewState["listtxtunit4ctrl"];

        listtdunitrate1ctrl = (List<string>)ViewState["listtdunitrate1ctrl"];
        listtxtunitrate1ctrl = (List<string>)ViewState["listtxtunitrate1ctrl"];
        listtdunitrate2ctrl = (List<string>)ViewState["listtdunitrate2ctrl"];
        listtxtunitrate2ctrl = (List<string>)ViewState["listtxtunitrate2ctrl"];

        listtdgrossamtctrl = (List<string>)ViewState["listtdgrossamtctrl"];
        listtxtgrossamtctrl = (List<string>)ViewState["listtxtgrossamtctrl"];

        listtddiscount1ctrl = (List<string>)ViewState["listtddiscount1ctrl"];
        listtxtdiscount1ctrl = (List<string>)ViewState["listtxtdiscount1ctrl"];

        listtddiscount2ctrl = (List<string>)ViewState["listtddiscount2ctrl"];
        listtxtdiscount2ctrl = (List<string>)ViewState["listtxtdiscount2ctrl"];

        listtdservicecharge1crtl = (List<string>)ViewState["listtdservicecharge1crtl"];
        listhidServiceCharge = (List<string>)ViewState["listhidServiceCharge"];
        listtxtservicecharge1crtl = (List<string>)ViewState["listtxtservicecharge1crtl"];
        listtdservicecharge2crtl = (List<string>)ViewState["listtdservicecharge2crtl"];
        listtxtservicecharge2crtl = (List<string>)ViewState["listtxtservicecharge2crtl"];
        listtdservicecharge3crtl = (List<string>)ViewState["listtdservicecharge3crtl"];
        listtxtservicecharge3crtl = (List<string>)ViewState["listtxtservicecharge3crtl"];

        listtdserviceTax1crtl = (List<string>)ViewState["listtdserviceTax1crtl"];
        listtxtserviceTax1crtl = (List<string>)ViewState["listtxtserviceTax1crtl"];
        listtdserviceTax2crtl = (List<string>)ViewState["listtdserviceTax2crtl"];
        listtxtserviceTax2crtl = (List<string>)ViewState["listtxtserviceTax2crtl"];
        listtdserviceTax3crtl = (List<string>)ViewState["listtdserviceTax3crtl"];
        listtxtserviceTax3crtl = (List<string>)ViewState["listtxtserviceTax3crtl"];
        listtdserviceTax4crtl = (List<string>)ViewState["listtdserviceTax4crtl"];
        listtxtserviceTax4crtl = (List<string>)ViewState["listtxtserviceTax4crtl"];
        listtdserviceTax5crtl = (List<string>)ViewState["listtdserviceTax5crtl"];
        listtxtserviceTax5crtl = (List<string>)ViewState["listtxtserviceTax5crtl"];

        listtdTaxincGrandTotalctrl = (List<string>)ViewState["listtdTaxincGrandTotalctrl"];
        listtxtTaxincGrandTotalctrl = (List<string>)ViewState["listtxtTaxincGrandTotalctrl"];
        LstChkDelete = (List<string>)ViewState["LstChkDelete"];
        LsthidChildID = (List<string>)ViewState["LsthidChildID"];
        if (listcounter != null)
        {
            int tempcounter = Convert.ToInt32(listcounter.Count);
            for (int i = 0; i < tempcounter; i++)
            {
                counter++;

                HtmlTableRow row = new HtmlTableRow();

                HtmlTableCell cell0 = new HtmlTableCell();
                cell0.Align = "Center";
                cell0.Width = "20px";

                CheckBox chkDelete = new CheckBox();
                chkDelete.ID = LstChkDelete[i].ToString();
                chkDelete.Width = Unit.Pixel(20);
                chkDelete.Attributes.Add("onclick", "checkAlLChecked();CalculateGrandTotal(this.id);");
                cell0.Controls.Add(chkDelete);

                HiddenField hidChildID = new HiddenField();
                hidChildID.ID = LsthidChildID[i].ToString();
                hidChildID.Value = "0";
                cell0.Controls.Add(hidChildID);

                HtmlTableCell cell1 = new HtmlTableCell();
                cell1.ID = listtdservicectrl[i].ToString();
                cell1.Align = "Center";
                cell1.Width = "150px";

                DropDownList ddlservice = new DropDownList();
                ddlservice.ID = listddlservice[i].ToString();
                ddlservice.CssClass = "dropdown";
                ddlservice.Width = Unit.Pixel(150);
                BindService(ddlservice);
                ddlservice.AutoPostBack = true;
                ddlservice.Attributes.Add("onchange", "return ValidateRequired(this.id);");
                ddlservice.SelectedIndexChanged += new EventHandler(ddlservice_SelectedIndexChanged);

                HiddenField hidTaxableNonTaxable = new HiddenField();
                hidTaxableNonTaxable.ID = listhidTaxableNonTaxable[i].ToString();

                HiddenField hidserviceLid = new HiddenField();
                hidserviceLid.ID = listhidserviceLid[i].ToString();

                HtmlTableCell cell2 = new HtmlTableCell();
                cell2.ID = listtddescriptionctrl[i].ToString();
                cell2.Align = "Center";
                cell2.Width = "120px";
                cell2.Style.Add("display", "none");


                TextBox txtdescriptionctrl = new TextBox();
                txtdescriptionctrl.ID = listtxtdescriptionctrl[i].ToString();
                txtdescriptionctrl.MaxLength = 248;
                txtdescriptionctrl.Width = Unit.Pixel(120);
                txtdescriptionctrl.Style.Add("display", "none");
                txtdescriptionctrl.CssClass = "input";

                HtmlTableCell cell3 = new HtmlTableCell();
                cell3.ID = listtdyearrefctrl[i].ToString();
                cell3.Align = "Center";
                cell3.Width = "70px";
                cell3.Style.Add("display", "none");

                TextBox txtyearrefctrl = new TextBox();
                txtyearrefctrl.ID = listtxtyearrefctrl[i].ToString();
                txtyearrefctrl.Width = Unit.Pixel(70);
                txtyearrefctrl.MaxLength = 48;
                txtyearrefctrl.Style.Add("display", "none");
                txtyearrefctrl.CssClass = "input";

                HtmlTableCell cell4 = new HtmlTableCell();
                cell4.ID = listtduomctrl[i].ToString();
                cell4.Align = "Center";
                cell4.Width = "70px";
                cell4.Style.Add("display", "none");

                TextBox txtuomctrl = new TextBox();
                txtuomctrl.ID = listtxtuomctrl[i].ToString();
                txtuomctrl.Width = Unit.Pixel(70);
                txtuomctrl.MaxLength = 25;
                txtuomctrl.Style.Add("display", "none");
                txtuomctrl.CssClass = "input";

                HtmlTableCell cell5 = new HtmlTableCell();
                cell5.ID = listtdunit2ctrl[i].ToString();
                cell5.Align = "Center";
                cell5.Width = "70px";
                cell5.Style.Add("display", "none");

                TextBox txtunit2ctrl = new TextBox();
                txtunit2ctrl.ID = listtxtunit2ctrl[i].ToString();
                txtunit2ctrl.Width = Unit.Pixel(70);
                txtunit2ctrl.MaxLength = 10;
                txtunit2ctrl.Style.Add("display", "none");
                txtunit2ctrl.CssClass = "inputnumber";
                txtunit2ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");

                HtmlTableCell cell6 = new HtmlTableCell();
                cell6.ID = listtdunit3ctrl[i].ToString();
                cell6.Align = "Center";
                cell6.Width = "70px";
                cell6.Style.Add("display", "none");

                TextBox txtunit3ctrl = new TextBox();
                txtunit3ctrl.ID = listtxtunit3ctrl[i].ToString();
                txtunit3ctrl.Width = Unit.Pixel(70);
                txtunit3ctrl.MaxLength = 10;
                txtunit3ctrl.Style.Add("display", "none");
                txtunit3ctrl.CssClass = "inputnumber";
                txtunit3ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");

                HtmlTableCell cell7 = new HtmlTableCell();
                cell7.ID = listtdunit4ctrl[i].ToString();
                cell7.Align = "Center";
                cell7.Width = "70px";
                cell7.Style.Add("display", "none");

                TextBox txtunit4ctrl = new TextBox();
                txtunit4ctrl.ID = listtxtunit4ctrl[i].ToString();
                txtunit4ctrl.Width = Unit.Pixel(70);
                txtunit4ctrl.MaxLength = 10;
                txtunit4ctrl.Style.Add("display", "none");
                txtunit4ctrl.CssClass = "inputnumber";
                txtunit4ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");

                HtmlTableCell cell8 = new HtmlTableCell();
                cell8.ID = listtdunitrate1ctrl[i].ToString();
                cell8.Align = "Center";
                cell8.Width = "70px";
                cell8.Style.Add("display", "none");

                TextBox txtunitrate1ctrl = new TextBox();
                txtunitrate1ctrl.ID = listtxtunitrate1ctrl[i].ToString();
                txtunitrate1ctrl.Width = Unit.Pixel(70);
                txtunitrate1ctrl.MaxLength = 10;
                txtunitrate1ctrl.Style.Add("display", "none");
                txtunitrate1ctrl.CssClass = "inputnumber";
                txtunitrate1ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");

                HtmlTableCell cell9 = new HtmlTableCell();
                cell9.ID = listtdunitrate2ctrl[i].ToString();
                cell9.Align = "Center";
                cell9.Width = "70px";
                cell9.Style.Add("display", "none");

                TextBox txtunitrate2ctrl = new TextBox();
                txtunitrate2ctrl.ID = listtxtunitrate2ctrl[i].ToString();
                txtunitrate2ctrl.Width = Unit.Pixel(70);
                txtunitrate2ctrl.MaxLength = 10;
                txtunitrate2ctrl.Style.Add("display", "none");
                txtunitrate2ctrl.CssClass = "inputnumber";
                txtunitrate2ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");
                txtunitrate2ctrl.Attributes.Add("onBlur", "return CalculateGrandTotal(this.id);");

                HtmlTableCell cell10 = new HtmlTableCell();
                cell10.ID = listtdgrossamtctrl[i].ToString();
                cell10.Align = "Center";
                cell10.Width = "120px";
                //cell10.style.Add("display","none");

                TextBox txtgrossamtctrl = new TextBox();
                txtgrossamtctrl.ID = listtxtgrossamtctrl[i].ToString();
                txtgrossamtctrl.Width = Unit.Pixel(120);
                //txtgrossamtctrl.style.Add("display","none");
                txtgrossamtctrl.CssClass = "inputnumber";
                txtgrossamtctrl.Attributes.Add("OnKeyDown", "return isNumberKey(this.id,event);");
                txtgrossamtctrl.Attributes.Add("onBlur", "return CalculateGrandTotal(this.id);");

                HiddenField hidServiceCharge = new HiddenField();
                hidServiceCharge.ID = listhidServiceCharge[i].ToString();

                HtmlTableCell cell11 = new HtmlTableCell();
                cell11.ID = listtddiscount1ctrl[i].ToString();
                cell11.Align = "Center";
                cell11.Width = "80px";
                cell11.Style.Add("display", "none");

                TextBox txtdiscount1ctrl = new TextBox();
                txtdiscount1ctrl.ID = listtxtdiscount1ctrl[i].ToString();
                txtdiscount1ctrl.Width = Unit.Pixel(80);
                txtdiscount1ctrl.Style.Add("display", "none");
                txtdiscount1ctrl.CssClass = "inputnumber";
                txtdiscount1ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");

                HtmlTableCell cell12 = new HtmlTableCell();
                cell12.ID = listtddiscount2ctrl[i].ToString();
                cell12.Align = "Center";
                cell12.Width = "120px";
                cell12.Style.Add("display", "none");

                TextBox txtdiscount2ctrl = new TextBox();
                txtdiscount2ctrl.ID = listtxtdiscount2ctrl[i].ToString();
                txtdiscount2ctrl.Width = Unit.Pixel(120);
                txtdiscount2ctrl.Style.Add("display", "none");
                txtdiscount2ctrl.CssClass = "DisableInputNumber";
                txtdiscount2ctrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");

                HtmlTableCell cell13 = new HtmlTableCell();
                cell13.ID = listtdservicecharge1crtl[i].ToString();
                cell13.Align = "Center";
                cell13.Width = "120px";
                cell13.Style.Add("display", "none");

                TextBox txtservicecharge1crtl = new TextBox();
                txtservicecharge1crtl.ID = listtxtservicecharge1crtl[i].ToString();
                txtservicecharge1crtl.Width = Unit.Pixel(120);
                txtservicecharge1crtl.Style.Add("display", "none");
                txtservicecharge1crtl.CssClass = "inputnumber";
                txtservicecharge1crtl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");
                txtservicecharge1crtl.Attributes.Add("onBlur", "return CalculateGrandTotal(this.id);");


                HtmlTableCell cell14 = new HtmlTableCell();
                cell14.ID = listtdservicecharge2crtl[i].ToString();
                cell14.Align = "Center";
                cell14.Width = "100px";
                cell14.Style.Add("display", "none");

                TextBox txtservicecharge2crtl = new TextBox();
                txtservicecharge2crtl.ID = listtxtservicecharge2crtl[i].ToString();
                txtservicecharge2crtl.Width = Unit.Pixel(100);
                txtservicecharge2crtl.Style.Add("display", "none");
                txtservicecharge2crtl.CssClass = "DisableInputNumber";
                txtservicecharge2crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");

                HtmlTableCell cell15 = new HtmlTableCell();
                cell15.ID = listtdservicecharge3crtl[i].ToString();
                cell15.Align = "Center";
                cell15.Width = "100px";
                cell15.Style.Add("display", "none");

                TextBox txtservicecharge3crtl = new TextBox();
                txtservicecharge3crtl.ID = listtxtservicecharge3crtl[i].ToString();
                txtservicecharge3crtl.Width = Unit.Pixel(100);
                txtservicecharge3crtl.Style.Add("display", "none");
                txtservicecharge3crtl.CssClass = "DisableInputNumber";
                txtservicecharge3crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");

                HtmlTableCell cell16 = new HtmlTableCell();
                cell16.ID = listtdserviceTax1crtl[i].ToString();
                cell16.Align = "Center";
                cell16.Width = "100px";
                cell16.Style.Add("display", "none");

                TextBox txtserviceTax1crtl = new TextBox();
                txtserviceTax1crtl.ID = listtxtserviceTax1crtl[i].ToString();
                txtserviceTax1crtl.Width = Unit.Pixel(100);
                txtserviceTax1crtl.Style.Add("display", "none");
                txtserviceTax1crtl.CssClass = "DisableInputNumber";
                txtserviceTax1crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");

                HtmlTableCell cell17 = new HtmlTableCell();
                cell17.ID = listtdserviceTax2crtl[i].ToString();
                cell17.Align = "Center";
                cell17.Width = "70px";
                cell17.Style.Add("display", "none");

                TextBox txtserviceTax2crtl = new TextBox();
                txtserviceTax2crtl.ID = listtxtserviceTax2crtl[i].ToString();
                txtserviceTax2crtl.Width = Unit.Pixel(70);
                txtserviceTax2crtl.Style.Add("display", "none");
                txtserviceTax2crtl.CssClass = "DisableInputNumber";
                txtserviceTax2crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");

                HtmlTableCell cell18 = new HtmlTableCell();
                cell18.ID = listtdserviceTax3crtl[i].ToString();
                cell18.Align = "Center";
                cell18.Width = "70px";
                cell18.Style.Add("display", "none");

                TextBox txtserviceTax3crtl = new TextBox();
                txtserviceTax3crtl.ID = listtxtserviceTax3crtl[i].ToString();
                txtserviceTax3crtl.Width = Unit.Pixel(70);
                txtserviceTax3crtl.Style.Add("display", "none");
                txtserviceTax3crtl.CssClass = "DisableInputNumber";
                txtserviceTax3crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");

                HtmlTableCell cell19 = new HtmlTableCell();
                cell19.ID = listtdserviceTax4crtl[i].ToString();
                cell19.Align = "Center";
                cell19.Width = "70px";
                cell19.Style.Add("display", "none");

                TextBox txtserviceTax4crtl = new TextBox();
                txtserviceTax4crtl.ID = listtxtserviceTax4crtl[i].ToString();
                txtserviceTax4crtl.Width = Unit.Pixel(70);
                txtserviceTax4crtl.Style.Add("display", "none");
                txtserviceTax4crtl.CssClass = "DisableInputNumber";
                txtserviceTax4crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");

                HtmlTableCell cell20 = new HtmlTableCell();
                cell20.ID = listtdserviceTax5crtl[i].ToString();
                cell20.Align = "Center";
                cell20.Width = "70px";
                cell20.Style.Add("display", "none");

                TextBox txtserviceTax5crtl = new TextBox();
                txtserviceTax5crtl.ID = listtxtserviceTax5crtl[i].ToString();
                txtserviceTax5crtl.Width = Unit.Pixel(70);
                txtserviceTax5crtl.Style.Add("display", "none");
                txtserviceTax5crtl.CssClass = "DisableInputNumber";
                txtserviceTax5crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");

                HtmlTableCell cell21 = new HtmlTableCell();
                cell21.ID = listtdTaxincGrandTotalctrl[i].ToString();
                cell21.Align = "Center";
                cell21.Width = "120px";
                cell21.Style.Add("display", "none");

                TextBox txtTaxincGrandTotalctrl = new TextBox();
                txtTaxincGrandTotalctrl.ID = listtxtTaxincGrandTotalctrl[i].ToString();
                txtTaxincGrandTotalctrl.Width = Unit.Pixel(120);
                txtTaxincGrandTotalctrl.Style.Add("display", "none");
                txtTaxincGrandTotalctrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");
                txtTaxincGrandTotalctrl.Attributes.Add("onBlur", "return CalculateGrandTotal(this.id);");
                txtTaxincGrandTotalctrl.CssClass = "DisableInputNumber";

                cell1.Controls.Add(ddlservice);
                cell1.Controls.Add(hidTaxableNonTaxable);
                cell1.Controls.Add(hidserviceLid);
                cell2.Controls.Add(txtdescriptionctrl);
                cell3.Controls.Add(txtyearrefctrl);
                cell4.Controls.Add(txtuomctrl);
                cell5.Controls.Add(txtunit2ctrl);
                cell6.Controls.Add(txtunit3ctrl);
                cell7.Controls.Add(txtunit4ctrl);
                cell8.Controls.Add(txtunitrate1ctrl);
                cell9.Controls.Add(txtunitrate2ctrl);
                cell10.Controls.Add(txtgrossamtctrl);
                cell10.Controls.Add(hidServiceCharge);
                cell11.Controls.Add(txtdiscount1ctrl);
                cell12.Controls.Add(txtdiscount2ctrl);
                cell13.Controls.Add(txtservicecharge1crtl);
                cell14.Controls.Add(txtservicecharge2crtl);
                cell15.Controls.Add(txtservicecharge3crtl);
                cell16.Controls.Add(txtserviceTax1crtl);
                cell17.Controls.Add(txtserviceTax2crtl);
                cell18.Controls.Add(txtserviceTax3crtl);
                cell19.Controls.Add(txtserviceTax4crtl);
                cell20.Controls.Add(txtserviceTax5crtl);
                cell21.Controls.Add(txtTaxincGrandTotalctrl);

                row.Cells.Add(cell0);
                row.Cells.Add(cell1);
                row.Cells.Add(cell2);
                row.Cells.Add(cell3);
                row.Cells.Add(cell4);
                row.Cells.Add(cell5);
                row.Cells.Add(cell6);
                row.Cells.Add(cell7);
                row.Cells.Add(cell8);
                row.Cells.Add(cell9);
                row.Cells.Add(cell10);
                row.Cells.Add(cell11);
                row.Cells.Add(cell12);
                row.Cells.Add(cell13);
                row.Cells.Add(cell14);
                row.Cells.Add(cell15);
                row.Cells.Add(cell16);
                row.Cells.Add(cell17);
                row.Cells.Add(cell18);
                row.Cells.Add(cell19);
                row.Cells.Add(cell20);
                row.Cells.Add(cell21);

                tblmain.Rows.Add(row);
            }

        }
    }
    protected void BindCustomerPrefence()
    {
        if (ddlCustomer.SelectedValue != "0" && ddlFormat.SelectedValue != "0")
        {

            SqlParameter[] parma = {       //// Get CustomerPreferenceid and Credit Days for PreLoad Rows by Customer PreferenceCild Table
                                new SqlParameter("@CUSTOMERLEDGERID",ddlCustomer.SelectedValue),
                                new SqlParameter("@FORMATID",ddlFormat.SelectedValue)
                               };
            DataTable dtCustomerprefid = DU.getdatausingprocedure("USP_IVC_GetCustomerPreference", parma);
            if (dtCustomerprefid.Rows.Count > 0)
            {
                if (Convert.ToInt32(dtCustomerprefid.Rows[0]["CUSTOMERPREID"]) > 0)
                {
                    hidCustomerprefid.Value = dtCustomerprefid.Rows[0]["CUSTOMERPREID"].ToString();
                    if (dtCustomerprefid.Rows[0]["CRDAYS"].ToString() != string.Empty)
                    {
                        txtcrdays.Text = dtCustomerprefid.Rows[0]["CRDAYS"].ToString();
                    }
                    else
                    {
                        txtcrdays.Text = "0";
                    }
                }
            }
            if (hidCustomerprefid.Value != string.Empty)
            {
                SqlParameter[] parmaa = { 
                                 new SqlParameter("@CUSTOMERPREID",hidCustomerprefid.Value)
                                    };
                DataTable dtPreLoadRows = DU.getdatausingprocedure("USP_IVC_GetCustomerPreferenceServices", parmaa);
                if (dtPreLoadRows.Rows.Count > 0)
                {
                    if (dtPreLoadRows.Rows.Count > 2)
                    {
                        for (int i = 2; i < dtPreLoadRows.Rows.Count; i++)
                        {
                            ADDMoreRow();
                        }
                    }
                    GetDynamicColumn();
                    BindServiceRows(dtPreLoadRows);
                }
            }
        }
    }
    protected void BindServiceRows(DataTable dtPreLoadRows)
    {
        SqlParameter[] parma = { 
                                new SqlParameter("@Formatid",ddlFormat.SelectedValue)
                               };
        DataTable dtcol = DU.getdatausingprocedure("USP_IVC_GetTransactionFormat_EnableDisable", parma);
        if (dtPreLoadRows.Rows.Count > 0)
        {
            grid.DataSource = dtPreLoadRows;
            grid.DataBind();
            for (int i = 1; i <= dtPreLoadRows.Rows.Count; i++)
            {
                DropDownList ddlservice = (DropDownList)tblmain.Rows[i].FindControl("ddlservice" + i.ToString());
                ddlservice.SelectedValue = Convert.ToString(dtPreLoadRows.Rows[i - 1]["SERVICEID"]);
                ddlservice_SelectedIndexChanged(ddlservice, EventArgs.Empty);

                HiddenField hidTaxableNonTaxable = (HiddenField)tblmain.Rows[i].FindControl("hidTaxableNonTaxable" + i.ToString());
                if (dtcol.Rows[0]["Adddesc"].ToString() == "True")
                {
                    TextBox txtdescriptionctrl = (TextBox)tblmain.Rows[i].FindControl("txtdescriptionctrl" + i.ToString());
                }
                if (dtcol.Rows[0]["Yearreference"].ToString() == "True")
                {
                    TextBox txtyearrefctrl = (TextBox)tblmain.Rows[i].FindControl("txtyearrefctrl" + i.ToString());
                }
                if (dtcol.Rows[0]["uom"].ToString() == "True")
                {
                    TextBox txtuomctrl = (TextBox)tblmain.Rows[i].FindControl("txtuomctrl" + i.ToString());
                    txtuomctrl.Text = Convert.ToString(dtPreLoadRows.Rows[i - 1]["UOM"]);
                }
                if (dtcol.Rows[0]["unit2"].ToString() == "True")
                {
                    TextBox txtunit2ctrl = (TextBox)tblmain.Rows[i].FindControl("txtunit2ctrl" + i.ToString());
                    txtunit2ctrl.Text = Convert.ToString(dtPreLoadRows.Rows[i - 1]["UNIT2"]);
                }
                if (dtcol.Rows[0]["unit3"].ToString() == "True")
                {
                    TextBox txtunit3ctrl = (TextBox)tblmain.Rows[i].FindControl("txtunit3ctrl" + i.ToString());
                    txtunit3ctrl.Text = Convert.ToString(dtPreLoadRows.Rows[i - 1]["UNIT3"]);
                }
                if (dtcol.Rows[0]["unit4"].ToString() == "True")
                {
                    TextBox txtunit4ctrl = (TextBox)tblmain.Rows[i].FindControl("txtunit4ctrl" + i.ToString());
                    txtunit4ctrl.Text = Convert.ToString(dtPreLoadRows.Rows[i - 1]["UNIT4"]);
                }
                if (dtcol.Rows[0]["unitrate"].ToString() == "True")
                {
                    TextBox txtunitrate1ctrl = (TextBox)tblmain.Rows[i].FindControl("txtunitrate1ctrl" + i.ToString());

                    txtunitrate1ctrl.Text = Convert.ToString(dtPreLoadRows.Rows[i - 1]["UNIT"]);

                    TextBox txtunitrate2ctrl = (TextBox)tblmain.Rows[i].FindControl("txtunitrate2ctrl" + i.ToString());

                    txtunitrate2ctrl.Text = Convert.ToString(dtPreLoadRows.Rows[i - 1]["RATE"]);
                }
                TextBox txtgrossamtctrl = (TextBox)tblmain.Rows[i].FindControl("txtgrossamtctrl" + i.ToString());
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script" + i.ToString() + "", "CalculateGrandTotal('" + txtgrossamtctrl.ID + "');", true);
                if (dtcol.Rows[0]["Discountlevel"].ToString() == "2")
                {
                    TextBox txtdiscount1ctrl = (TextBox)tblmain.Rows[i].FindControl("txtdiscount1ctrl" + i.ToString());
                    TextBox txtdiscount2ctrl = (TextBox)tblmain.Rows[i].FindControl("txtdiscount2ctrl" + i.ToString());
                }
                if (dtcol.Rows[0]["ShowserviceCharge"].ToString() == "True")
                {
                    TextBox txtservicecharge1crtl = (TextBox)tblmain.Rows[i].FindControl("txtservicecharge1crtl" + i.ToString());
                    TextBox txtservicecharge2crtl = (TextBox)tblmain.Rows[i].FindControl("txtservicecharge2crtl" + i.ToString());
                    TextBox txtservicecharge3crtl = (TextBox)tblmain.Rows[i].FindControl("txtservicecharge3crtl" + i.ToString());
                }
                if (dtcol.Rows[0]["Taxlevel"].ToString() == "True")
                {
                    TextBox txtserviceTax1crtl = (TextBox)tblmain.Rows[i].FindControl("txtserviceTax1crtl" + i.ToString());
                    TextBox txtserviceTax2crtl = (TextBox)tblmain.Rows[i].FindControl("txtserviceTax2crtl" + i.ToString());
                    TextBox txtserviceTax3crtl = (TextBox)tblmain.Rows[i].FindControl("txtserviceTax3crtl" + i.ToString());
                    if (dtcol.Rows[0]["ItemTaxType"].ToString() == "1")
                    {
                        TextBox txtserviceTax4crtl = (TextBox)tblmain.Rows[i].FindControl("txtserviceTax4crtl" + i.ToString());
                    }
                    else if (dtcol.Rows[0]["ItemTaxType"].ToString() == "2")
                    {
                        TextBox txtserviceTax5crtl = (TextBox)tblmain.Rows[i].FindControl("txtserviceTax5crtl" + i.ToString());
                    }
                }
                if (dtcol.Rows[0]["Taxinclusive"].ToString() == "True" || dtcol.Rows[0]["ItemTaxType"].ToString() == "0")
                {
                    TextBox txtTaxincGrandTotalctrl = (TextBox)tblmain.Rows[i].FindControl("txtTaxincGrandTotalctrl" + i.ToString());
                }
            }

        }
    }
    protected void ClearTable()
    {
        listcounter = (List<string>)ViewState["listcounter"];

        listtdservicectrl = (List<string>)ViewState["listtdservicectrl"];
        listddlservice = (List<string>)ViewState["listddlservice"];

        listtddescriptionctrl = (List<string>)ViewState["listtddescriptionctrl"];
        listtxtdescriptionctrl = (List<string>)ViewState["listtxtdescriptionctrl"];

        listtdyearrefctrl = (List<string>)ViewState["listtdyearrefctrl"];
        listtxtyearrefctrl = (List<string>)ViewState["listtxtyearrefctrl"];

        listtduomctrl = (List<string>)ViewState["listtduomctrl"];
        listtxtuomctrl = (List<string>)ViewState["listtxtuomctrl"];

        listtdunit2ctrl = (List<string>)ViewState["listtdunit2ctrl"];
        listtxtunit2ctrl = (List<string>)ViewState["listtxtunit2ctrl"];
        listtdunit3ctrl = (List<string>)ViewState["listtdunit3ctrl"];
        listtxtunit3ctrl = (List<string>)ViewState["listtxtunit3ctrl"];
        listtdunit4ctrl = (List<string>)ViewState["listtdunit4ctrl"];
        listtxtunit4ctrl = (List<string>)ViewState["listtxtunit4ctrl"];

        listtdunitrate1ctrl = (List<string>)ViewState["listtdunitrate1ctrl"];
        listtxtunitrate1ctrl = (List<string>)ViewState["listtxtunitrate1ctrl"];
        listtdunitrate2ctrl = (List<string>)ViewState["listtdunitrate2ctrl"];
        listtxtunitrate2ctrl = (List<string>)ViewState["listtxtunitrate2ctrl"];

        listtdgrossamtctrl = (List<string>)ViewState["listtdgrossamtctrl"];
        listtxtgrossamtctrl = (List<string>)ViewState["listtxtgrossamtctrl"];

        listtddiscount1ctrl = (List<string>)ViewState["listtddiscount1ctrl"];
        listtxtdiscount1ctrl = (List<string>)ViewState["listtxtdiscount1ctrl"];

        listtddiscount2ctrl = (List<string>)ViewState["listtddiscount2ctrl"];
        listtxtdiscount2ctrl = (List<string>)ViewState["listtxtdiscount2ctrl"];

        listtdservicecharge1crtl = (List<string>)ViewState["listtdservicecharge1crtl"];
        listtxtservicecharge1crtl = (List<string>)ViewState["listtxtservicecharge1crtl"];
        listtdservicecharge2crtl = (List<string>)ViewState["listtdservicecharge2crtl"];
        listtxtservicecharge2crtl = (List<string>)ViewState["listtxtservicecharge2crtl"];
        listtdservicecharge3crtl = (List<string>)ViewState["listtdservicecharge3crtl"];
        listtxtservicecharge3crtl = (List<string>)ViewState["listtxtservicecharge3crtl"];

        listtdserviceTax1crtl = (List<string>)ViewState["listtdserviceTax1crtl"];
        listtxtserviceTax1crtl = (List<string>)ViewState["listtxtserviceTax1crtl"];
        listtdserviceTax2crtl = (List<string>)ViewState["listtdserviceTax2crtl"];
        listtxtserviceTax2crtl = (List<string>)ViewState["listtxtserviceTax2crtl"];
        listtdserviceTax3crtl = (List<string>)ViewState["listtdserviceTax3crtl"];
        listtxtserviceTax3crtl = (List<string>)ViewState["listtxtserviceTax3crtl"];
        listtdserviceTax4crtl = (List<string>)ViewState["listtdserviceTax4crtl"];
        listtxtserviceTax4crtl = (List<string>)ViewState["listtxtserviceTax4crtl"];
        listtdserviceTax5crtl = (List<string>)ViewState["listtdserviceTax5crtl"];
        listtxtserviceTax5crtl = (List<string>)ViewState["listtxtserviceTax5crtl"];
        listhidTaxableNonTaxable = (List<string>)ViewState["listhidTaxableNonTaxable"];
        listhidserviceLid = (List<string>)ViewState["listhidserviceLid"];
        listhidServiceCharge = (List<string>)ViewState["listhidServiceCharge"];
        LsthidChildID = (List<string>)ViewState["LsthidChildID"];
        LstChkDelete = (List<string>)ViewState["LstChkDelete"];
        listtdTaxincGrandTotalctrl = (List<string>)ViewState["listtdTaxincGrandTotalctrl"];
        listtxtTaxincGrandTotalctrl = (List<string>)ViewState["listtxtTaxincGrandTotalctrl"];
        if (tblmain.Rows.Count > 2)
        {
            for (int i = tblmain.Rows.Count; i > 2; i--)
            {
                if (listcounter != null)
                {
                    listcounter.RemoveAt(i - 3);

                    listtdservicectrl.RemoveAt(i - 3);
                    listddlservice.RemoveAt(i - 3);

                    listtddescriptionctrl.RemoveAt(i - 3);
                    listtxtdescriptionctrl.RemoveAt(i - 3);
                    listtdyearrefctrl.RemoveAt(i - 3);
                    listtxtyearrefctrl.RemoveAt(i - 3);

                    listtduomctrl.RemoveAt(i - 3);
                    listtxtuomctrl.RemoveAt(i - 3);

                    listtdunit2ctrl.RemoveAt(i - 3);
                    listtxtunit2ctrl.RemoveAt(i - 3);
                    listtdunit3ctrl.RemoveAt(i - 3);
                    listtxtunit3ctrl.RemoveAt(i - 3);
                    listtdunit4ctrl.RemoveAt(i - 3);
                    listtxtunit4ctrl.RemoveAt(i - 3);

                    listtdunitrate1ctrl.RemoveAt(i - 3);
                    listtxtunitrate1ctrl.RemoveAt(i - 3);
                    listtdunitrate2ctrl.RemoveAt(i - 3);
                    listtxtunitrate2ctrl.RemoveAt(i - 3);

                    listtdgrossamtctrl.RemoveAt(i - 3);
                    listtxtgrossamtctrl.RemoveAt(i - 3);

                    listtddiscount1ctrl.RemoveAt(i - 3);
                    listtxtdiscount1ctrl.RemoveAt(i - 3);

                    listtddiscount2ctrl.RemoveAt(i - 3);
                    listtxtdiscount2ctrl.RemoveAt(i - 3);

                    listtdservicecharge1crtl.RemoveAt(i - 3);
                    listtxtservicecharge1crtl.RemoveAt(i - 3);
                    listtdservicecharge2crtl.RemoveAt(i - 3);
                    listtxtservicecharge2crtl.RemoveAt(i - 3);
                    listtdservicecharge3crtl.RemoveAt(i - 3);
                    listtxtservicecharge3crtl.RemoveAt(i - 3);

                    listtdserviceTax1crtl.RemoveAt(i - 3);
                    listtxtserviceTax1crtl.RemoveAt(i - 3);
                    listtdserviceTax2crtl.RemoveAt(i - 3);
                    listtxtserviceTax2crtl.RemoveAt(i - 3);
                    listtdserviceTax3crtl.RemoveAt(i - 3);
                    listtxtserviceTax3crtl.RemoveAt(i - 3);
                    listtdserviceTax4crtl.RemoveAt(i - 3);
                    listtxtserviceTax4crtl.RemoveAt(i - 3);
                    listtdserviceTax5crtl.RemoveAt(i - 3);
                    listtxtserviceTax5crtl.RemoveAt(i - 3);
                    listhidTaxableNonTaxable.RemoveAt(i - 3);
                    listtdTaxincGrandTotalctrl.RemoveAt(i - 3);
                    listtxtTaxincGrandTotalctrl.RemoveAt(i - 3);
                    listhidserviceLid.RemoveAt(i - 3);
                    listhidServiceCharge.RemoveAt(i - 3);
                    LsthidChildID.RemoveAt(i - 3);
                    LstChkDelete.RemoveAt(i - 3);

                }
                tblmain.Rows.RemoveAt(i - 1);
            }

            ViewState["listcounter"] = listcounter;

            ViewState["listtdservicectrl"] = listtdservicectrl;
            ViewState["listddlservice"] = listddlservice;
            ViewState["listhidTaxableNonTaxable"] = listhidTaxableNonTaxable;
            ViewState["listhidserviceLid"] = listhidserviceLid;

            ViewState["listtddescriptionctrl"] = listtddescriptionctrl;
            ViewState["listtxtdescriptionctrl"] = listtxtdescriptionctrl;

            ViewState["listtdyearrefctrl"] = listtdyearrefctrl;
            ViewState["listtxtyearrefctrl"] = listtxtyearrefctrl;

            ViewState["listtduomctrl"] = listtduomctrl;
            ViewState["listtxtuomctrl"] = listtxtuomctrl;

            ViewState["listtdunit2ctrl"] = listtdunit2ctrl;
            ViewState["listtxtunit2ctrl"] = listtxtunit2ctrl;
            ViewState["listtdunit3ctrl"] = listtdunit3ctrl;
            ViewState["listtxtunit3ctrl"] = listtxtunit3ctrl;
            ViewState["listtdunit4ctrl"] = listtdunit4ctrl;
            ViewState["listtxtunit4ctrl"] = listtxtunit4ctrl;

            ViewState["listtdunitrate1ctrl"] = listtdunitrate1ctrl;
            ViewState["listtxtunitrate1ctrl"] = listtxtunitrate1ctrl;
            ViewState["listtdunitrate2ctrl"] = listtdunitrate2ctrl;
            ViewState["listtxtunitrate2ctrl"] = listtxtunitrate2ctrl;

            ViewState["listtdgrossamtctrl"] = listtdgrossamtctrl;
            ViewState["listtxtgrossamtctrl"] = listtxtgrossamtctrl;

            ViewState["listtddiscount1ctrl"] = listtddiscount1ctrl;
            ViewState["listtxtdiscount1ctrl"] = listtxtdiscount1ctrl;

            ViewState["listtddiscount2ctrl"] = listtddiscount2ctrl;
            ViewState["listtxtdiscount2ctrl"] = listtxtdiscount2ctrl;

            ViewState["listtdservicecharge1crtl"] = listtdservicecharge1crtl;
            ViewState["listhidServiceCharge"] = listhidServiceCharge;
            ViewState["listtxtservicecharge1crtl"] = listtxtservicecharge1crtl;
            ViewState["listtdservicecharge2crtl"] = listtdservicecharge2crtl;
            ViewState["listtxtservicecharge2crtl"] = listtxtservicecharge2crtl;
            ViewState["listtdservicecharge3crtl"] = listtdservicecharge3crtl;
            ViewState["listtxtservicecharge3crtl"] = listtxtservicecharge3crtl;

            ViewState["listtdserviceTax1crtl"] = listtdserviceTax1crtl;
            ViewState["listtxtserviceTax1crtl"] = listtxtserviceTax1crtl;
            ViewState["listtdserviceTax2crtl"] = listtdserviceTax2crtl;
            ViewState["listtxtserviceTax2crtl"] = listtxtserviceTax2crtl;
            ViewState["listtdserviceTax3crtl"] = listtdserviceTax3crtl;
            ViewState["listtxtserviceTax3crtl"] = listtxtserviceTax3crtl;
            ViewState["listtdserviceTax4crtl"] = listtdserviceTax4crtl;
            ViewState["listtxtserviceTax4crtl"] = listtxtserviceTax4crtl;
            ViewState["listtdserviceTax5crtl"] = listtdserviceTax5crtl;
            ViewState["listtxtserviceTax5crtl"] = listtxtserviceTax5crtl;

            ViewState["listtdTaxincGrandTotalctrl"] = listtdTaxincGrandTotalctrl;
            ViewState["listtxtTaxincGrandTotalctrl"] = listtxtTaxincGrandTotalctrl;
            ViewState["LsthidChildID"] = LsthidChildID;
            ViewState["LstChkDelete"] = LstChkDelete;
            hcount.Value = "1";
            counter = 1;
            ResetTableControl(tblmain);
            chkDelete1.Checked = false;
        }
    }
    protected void ResetTableControl(Control Tableid)
    {
        foreach (Control c in Tableid.Controls)
        {
            if (c.GetType() == typeof(TextBox))
            {
                ((TextBox)(c)).Text = string.Empty;
            }
            if (c.GetType() == typeof(HiddenField))
            {
                ((HiddenField)(c)).Value = string.Empty;
            }
            if (c.GetType() == typeof(DropDownList))
            {
                ((DropDownList)(c)).SelectedIndex = 0;
            }
            if (c.HasControls())
            {
                ResetTableControl(c);
            }
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Server.Transfer("SalesInvoiceView.aspx");
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            Save_ACC_SaveVoucher();
            Response.Redirect("SalesInvoiceView.aspx");
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.StackTrace.ToString();
        }
    }
    protected void Save_ACC_SaveVoucher() // Save_ACC_SaveTransaction
    {
        if (hidTranType.Value != string.Empty)
        {
            string strTranDate = string.Empty;
            if (txtdate.Text != string.Empty)
            {
                string[] strTemp = txtdate.Text.Split('/');
                strTranDate = strTemp[1] + "/" + strTemp[0] + "/" + strTemp[2];
            }
            string strcurrdate = string.Empty;
            string[] temp = System.DateTime.Now.ToString().Split('/');
            strcurrdate = temp[1] + "/" + temp[0] + "/" + temp[2].ToString();
            SqlParameter[] parmavoucher = { 
                                       new SqlParameter("@TransactionID",ViewState["TRANID"].ToString()),
                                       new SqlParameter("@TransactionTypeID",hidTranType.Value.ToString()), 
                                       new SqlParameter("@TransactionNumber",txtbillno.Text),
                                       new SqlParameter("@FinYear",ViewState["finyear"].ToString()),
                                       new SqlParameter("@ReferenceNumber",txtrefno.Text),
                                       new SqlParameter("@IsCashVoucher","0"),
                                       new SqlParameter("@TransactionDate",strTranDate),
                                       new SqlParameter("@Narration",txtnarration.Text),
                                       new SqlParameter("@CreditDays",txtcrdays.Text),
                                       new SqlParameter("@ChequeNumber","0"),
                                       new SqlParameter("@ModifiedBy",Session["userid"].ToString()),
                                       new SqlParameter("@IsCancelled","0"),
                                       new SqlParameter("@CostCentreClassID","0")
                                      };
            int Result = DU.executeprocedure("USP_ACC_UpdateTransaction", parmavoucher);

            if (Result > 0)
            {
                Save_ACC_SaveVoucherDetails(ViewState["TRANID"].ToString());
            }
        }
    }
    protected void Save_ACC_SaveVoucherDetails(string TRANID) // Save_ACC_SaveTransactionDetails
    {
        if (TRANID != string.Empty)
        {
            string VouSerial = string.Empty;
            int count = Convert.ToInt32(hcount.Value);
            double grantotal = 0 - Convert.ToDouble(txtgrandtotal.Text);
            string strsql = "select * from ACC_VOUDETAILS where TRANID=" + TRANID;
            DataTable dt = DU.getdata(strsql);
            if (dt.Rows.Count > 0)
            {
                VouSerial = dt.Rows[0]["VOUSERIAL"].ToString();
            }
            SqlParameter[] parmavoucherdetails1 = {
                                       new SqlParameter("@TransactionSerial",Convert.ToInt32(VouSerial)),
                                       new SqlParameter("@TransactionID",TRANID),
                                       new SqlParameter("@LedgerID",Convert.ToString(ddlCustomer.SelectedValue)),
                                       new SqlParameter("@Amount",Convert.ToDecimal(grantotal)),//(0-Convert.ToInt32(txtgrandtotal.Text)).ToString()
                                       new SqlParameter("@IsReconciled","0"),
                                      };
            int result1 = DU.executeprocedure("USP_ACC_UpdateTransactionDetails", parmavoucherdetails1);
            int result2 = 0;

            for (int i = 1; i <= count; i++)
            {
                string serviceid = ((DropDownList)tblmain.Rows[i].FindControl("ddlservice" + i.ToString())).SelectedValue.ToString();
                if (serviceid != "0")
                {
                    string LID = ((HiddenField)tblmain.Rows[i].FindControl("hidserviceLid" + i.ToString())).Value.ToString();
                    string TotalAmount = ((TextBox)tblmain.Rows[i].FindControl("txtservicecharge3crtl" + i.ToString())).Text.ToString();

                    SqlParameter[] parmavoucherdetails2 = { 
                                       new SqlParameter("@TransactionSerial",Convert.ToInt32(VouSerial)+1),
                                       new SqlParameter("@TransactionID",TRANID),
                                       new SqlParameter("@LedgerID",LID),
                                       new SqlParameter("@Amount",TotalAmount),
                                       new SqlParameter("@IsReconciled","0"),
                                                          };
                    result2 = DU.executeprocedure("USP_ACC_UpdateTransactionDetails", parmavoucherdetails2);
                }
            }
            int result3 = 0;
            int result4 = 0;
            int result5 = 0;
            if (hidSaleLevel.Value == "ItemLevel")
            {
                SqlParameter[] parmavoucherdetails3 = {  // Service Tax
                                       new SqlParameter("@TransactionSerial",Convert.ToInt32(VouSerial)+2),
                                       new SqlParameter("@TransactionID",TRANID),
                                       new SqlParameter("@LedgerID",hidservicetaxpercent.Value),
                                       new SqlParameter("@Amount",txtservicetax.Text),
                                       new SqlParameter("@IsReconciled","0"),
                                      };
                result3 = DU.executeprocedure("USP_ACC_UpdateTransactionDetails", parmavoucherdetails3);
                SqlParameter[] parmavoucherdetails4 = {  // PECESS Tax
                                       new SqlParameter("@TransactionSerial",Convert.ToInt32(VouSerial)+3),
                                       new SqlParameter("@TransactionID",TRANID),
                                       new SqlParameter("@LedgerID",hidpecesspercent.Value),
                                       new SqlParameter("@Amount",txtpecess.Text),
                                       new SqlParameter("@IsReconciled","0"),
                                      };
                result4 = DU.executeprocedure("USP_ACC_UpdateTransactionDetails", parmavoucherdetails4);
                SqlParameter[] parmavoucherdetails5 = { // HECESS Tax
                                       new SqlParameter("@TransactionSerial",Convert.ToInt32(VouSerial)+4),
                                       new SqlParameter("@TransactionID",TRANID),
                                       new SqlParameter("@LedgerID",hidhecesspercent.Value),
                                       new SqlParameter("@Amount",txthecess.Text),
                                       new SqlParameter("@IsReconciled","0"),
                                      };
                result5 = DU.executeprocedure("USP_ACC_UpdateTransactionDetails", parmavoucherdetails5);
            }
            if (result1 > 0 && result2 > 0 || result3 > 0 || result4 > 0 || result5 > 0)
            {
                if (VouSerial != string.Empty)
                {
                    Save_ACC_REFDETAILS(VouSerial);
                    // Save_IVC_INVOICE_MAIN();
                }
            }
        }
    }
    protected void Save_ACC_REFDETAILS(string VouSerial)
    {
        if (VouSerial != string.Empty)
        {
            string Refserial = "";
            string strsql = "select REFSERIAL from ACC_REFDETAILS where VOUSERIAL=" + VouSerial;
            DataTable dtRefserial = DU.getdata(strsql);
            if (dtRefserial.Rows.Count > 0)
            {
                Refserial = dtRefserial.Rows[0]["REFSERIAL"].ToString();
            }
            string strRefDate = string.Empty;
            if (txtdate.Text != string.Empty)
            {
                string[] strTemp = txtdate.Text.Split('/');
                strRefDate = strTemp[1] + "/" + strTemp[0] + "/" + strTemp[2];
            }

            string Reference = string.Empty;
            //string strsql = "SELECT FORMATNAME FROM IVC_TRANSACTIONFORMAT WHERE FORMATID='" + ddlFormat.SelectedValue + "'";
            //DataTable DTFormatName = DU.getdata(strsql);
            //if (DTFormatName.Rows.Count > 0)
            //{
            if (txtcrdays.Text == string.Empty)
            {
                txtcrdays.Text = "0";
            }
            Reference += ddlFormat.SelectedItem.Text + " ";
            if (txtPrefix.Text != string.Empty)
            {
                Reference += Convert.ToString(txtPrefix.Text) + "/";
            }
            Reference += txtbillno.Text;
            if (txtSuffix.Text != string.Empty)
            {
                Reference += "/" + txtSuffix.Text;
            }
            //}
            double grantotal = 0 - Convert.ToDouble(txtgrandtotal.Text);
            SqlParameter[] pref = {
                                            new SqlParameter("@ReferenceSerial",Refserial),
                                            new SqlParameter("@ParentReference","0"),
                                            new SqlParameter("@TransactionSerial",VouSerial),
                                            new SqlParameter("@ReferenceType","1"),
                                            new SqlParameter("@Reference",Reference),
                                            new SqlParameter("@ReferenceDate",strRefDate),
                                            new SqlParameter("@CreditDays",Convert.ToString(txtcrdays.Text)),
                                            new SqlParameter("@ReferenceAmount",grantotal),//0-Convert.ToInt32(txtgrandtotal.Text)
                                            new SqlParameter("@IsTDSDeducted","0")
                                };
            int result = DU.executeprocedure("USP_ACC_SaveReferenceDetails", pref);
            if (result > 0)
            {
                Save_IVC_INVOICE_MAIN();
            }

        }
    }
    protected void Save_IVC_INVOICE_MAIN()
    {
        //if (TRANID != string.Empty)
        //{
        SqlParameter[] pmain = { 
                                new SqlParameter("@INVOICEID",ViewState["INVOICEID"].ToString()), // 0 For Insert
                                new SqlParameter("@TRANID",ViewState["TRANID"].ToString()),
                                new SqlParameter("@FORMATID",ddlFormat.SelectedValue),
                                new SqlParameter("@CUSTOMERLEDGERID",ddlCustomer.SelectedValue),
                                new SqlParameter("@SERVICEORDER",Convert.ToString(txtotherref.Text).Trim()),
                                new SqlParameter("@COMMENTS",Convert.ToString(txtremarks.Text.Trim())),
                                new SqlParameter("@CHECKEDBY",Convert.ToString(txtcheckedby.Text.Trim())),
                                new SqlParameter("@PREPAREDBY",Convert.ToString(txtpreparedby.Text.Trim())),
                                new SqlParameter("@SERVICEDBY",Convert.ToString(txtservicedby.Text.Trim())),
                                new SqlParameter("@ISTDSAPPLIED","0"),
                                new SqlParameter("@ISROUNDOFFEDITABLE","0")
                               };
        int result = DU.executeprocedure("USP_IVC_SaveInvoiceMain", pmain);
        if (result > 0)
        {
            Save_IVC_InvoiceChild(ViewState["INVOICEID"].ToString());
            Save_IVC_InvoiceTotal(ViewState["INVOICEID"].ToString());
        }
        //}
    }
    protected void Save_IVC_InvoiceChild(string InvoiceID)
    {
        if (InvoiceID != string.Empty)
        {
            int count = Convert.ToInt32(hcount.Value);
            for (int i = 1; i <= count; i++)
            {
                string serviceid = ((DropDownList)tblmain.Rows[i].FindControl("ddlservice" + i.ToString())).SelectedValue.ToString();
                CheckBox chkDelete = (CheckBox)tblmain.Rows[i].FindControl("chkDelete" + i.ToString());
                HiddenField hidChildID = (HiddenField)tblmain.Rows[i].FindControl("hidChildID" + i.ToString());
                if (serviceid != "0")
                {
                    if (chkDelete.Checked == true)
                    {
                        if (hidChildID.Value.ToString() != "0")
                        {
                            string strsql = "Delete from IVC_INVOICE_CHILD where INVOICECHILDID=" + hidChildID.Value.ToString();
                            DU.executesql(strsql);
                        }
                    }
                    else
                    {
                        string adddesc = ((TextBox)tblmain.Rows[i].FindControl("txtdescriptionctrl" + i.ToString())).Text.ToString();
                        string yearref = ((TextBox)tblmain.Rows[i].FindControl("txtyearrefctrl" + i.ToString())).Text.ToString();
                        string uom = ((TextBox)tblmain.Rows[i].FindControl("txtuomctrl" + i.ToString())).Text.ToString();
                        string unit2 = ((TextBox)tblmain.Rows[i].FindControl("txtunit2ctrl" + i.ToString())).Text.ToString();
                        string unit3 = ((TextBox)tblmain.Rows[i].FindControl("txtunit3ctrl" + i.ToString())).Text.ToString();
                        string unit4 = ((TextBox)tblmain.Rows[i].FindControl("txtunit4ctrl" + i.ToString())).Text.ToString();
                        string Unit = ((TextBox)tblmain.Rows[i].FindControl("txtunitrate1ctrl" + i.ToString())).Text.ToString();
                        string Rate = ((TextBox)tblmain.Rows[i].FindControl("txtunitrate2ctrl" + i.ToString())).Text.ToString();
                        string grossamt = ((TextBox)tblmain.Rows[i].FindControl("txtgrossamtctrl" + i.ToString())).Text.ToString();
                        string discountper = ((TextBox)tblmain.Rows[i].FindControl("txtdiscount1ctrl" + i.ToString())).Text.ToString();
                        string discountamt = ((TextBox)tblmain.Rows[i].FindControl("txtdiscount2ctrl" + i.ToString())).Text.ToString();
                        string serchvper = ((TextBox)tblmain.Rows[i].FindControl("txtservicecharge1crtl" + i.ToString())).Text.ToString();
                        string serchvamt = ((TextBox)tblmain.Rows[i].FindControl("txtservicecharge2crtl" + i.ToString())).Text.ToString();
                        string totalamt = ((TextBox)tblmain.Rows[i].FindControl("txtservicecharge3crtl" + i.ToString())).Text.ToString();
                        string servicetax_h = ((TextBox)tblmain.Rows[i].FindControl("txtserviceTax1crtl" + i.ToString())).Text.ToString();
                        string pecess_h = ((TextBox)tblmain.Rows[i].FindControl("txtserviceTax2crtl" + i.ToString())).Text.ToString();
                        string hecess_h = ((TextBox)tblmain.Rows[i].FindControl("txtserviceTax3crtl" + i.ToString())).Text.ToString();
                        string luxuary_h = ((TextBox)tblmain.Rows[i].FindControl("txtserviceTax4crtl" + i.ToString())).Text.ToString();
                        string vat_h = ((TextBox)tblmain.Rows[i].FindControl("txtserviceTax5crtl" + i.ToString())).Text.ToString();
                        string GrandTotal_h = ((TextBox)tblmain.Rows[i].FindControl("txtTaxincGrandTotalctrl" + i.ToString())).Text.ToString();

                        #region Fill Zero

                        if (unit2 == string.Empty)
                        {
                            unit2 = "0";
                        }
                        if (unit3 == string.Empty)
                        {
                            unit3 = "0";
                        }
                        if (unit4 == string.Empty)
                        {
                            unit4 = "0";
                        }
                        if (Unit == string.Empty)
                        {
                            Unit = "0";
                        }
                        if (Rate == string.Empty)
                        {
                            Rate = "0";
                        }
                        if (grossamt == string.Empty)
                        {
                            grossamt = "0";
                        }
                        if (discountper == string.Empty)
                        {
                            discountper = "0";
                        }
                        if (discountamt == string.Empty)
                        {
                            discountamt = "0";
                        }
                        if (serchvper == string.Empty)
                        {
                            serchvper = "0";
                        }
                        if (serchvamt == string.Empty)
                        {
                            serchvamt = "0";
                        }
                        if (totalamt == string.Empty)
                        {
                            totalamt = "0";
                        }
                        if (servicetax_h == string.Empty)
                        {
                            servicetax_h = "0";
                        }
                        if (pecess_h == string.Empty)
                        {
                            pecess_h = "0";
                        }
                        if (hecess_h == string.Empty)
                        {
                            hecess_h = "0";
                        }
                        if (luxuary_h == string.Empty)
                        {
                            luxuary_h = "0";
                        }
                        if (vat_h == string.Empty)
                        {
                            vat_h = "0";
                        }
                        if (GrandTotal_h == string.Empty)
                        {
                            GrandTotal_h = "0";
                        }
                        #endregion Fill Zero

                        double taxableafterdicount = 0;
                        taxableafterdicount = (Convert.ToDouble(grossamt) - Convert.ToDouble(discountamt));
                        SqlParameter[] pChild = { 
                                new SqlParameter("@INVOICECHILDID",hidChildID.Value.ToString()), 
                                new SqlParameter("@INVOICEID",InvoiceID),
                                new SqlParameter("@SERVICEID",serviceid),
                                new SqlParameter("@ADDDESC",adddesc),
                                new SqlParameter("@YEARREF",yearref),
                                new SqlParameter("@UNIT",Unit),
                                new SqlParameter("@RATE",Rate),
                                new SqlParameter("@TAXABLEAMT",grossamt),
                                new SqlParameter("@SERVICETAXAMT",servicetax_h),
                                new SqlParameter("@CESS1AMT",pecess_h),
                                new SqlParameter("@CESS2AMT",hecess_h),
                                new SqlParameter("@LTAMT",luxuary_h),
                                new SqlParameter("@DISCOUNT",discountamt),
                                new SqlParameter("@DISCOUNTRATE",discountper),
                                new SqlParameter("@SERVICECHARGERATE",serchvper),
                                new SqlParameter("@SERVICECHARGEAMT",serchvamt),
                                new SqlParameter("@ABTMNTAMT","0"),
                                new SqlParameter("@VATAMT",vat_h),
                                new SqlParameter("@TOTALAMT",GrandTotal_h),
                                new SqlParameter("@UNIT2",unit2),
                                new SqlParameter("@UNIT3",unit3),
                                new SqlParameter("@UNIT4",unit4),
                                new SqlParameter("@UOM",uom),
                                new SqlParameter("@TAXABLEAFTERDIS",taxableafterdicount.ToString())// (Convert.ToInt32(grossamt)-Convert.ToInt32(discountamt))
                               };
                        int result = DU.executeprocedure("USP_IVC_SaveInvoiceChild", pChild);
                    }
                }
            }
        }
    }
    protected void Save_IVC_InvoiceTotal(string InvoiceID)
    {
        if (InvoiceID != string.Empty)
        {
            #region Fill Zero
            if (txttaxableamount.Text.ToString() == string.Empty)
            {
                txttaxableamount.Text = "0";
            }
            if (txtnontaxableamount.Text.ToString() == string.Empty)
            {
                txtnontaxableamount.Text = "0";
            }
            if (txttotalamount.Text.ToString() == string.Empty)
            {
                txttotalamount.Text = "0";
            }
            if (txtservicetaxpercent.Text.ToString() == string.Empty)
            {
                txtservicetaxpercent.Text = "0";
            }
            if (txtservicetax.Text.ToString() == string.Empty)
            {
                txtservicetax.Text = "0";
            }
            if (txtpecess.Text.ToString() == string.Empty)
            {
                txtpecess.Text = "0";
            }
            if (txthecesspercent.Text.ToString() == string.Empty)
            {
                txthecesspercent.Text = "0";
            }
            if (txthecess.Text.ToString() == string.Empty)
            {
                txthecess.Text = "0";
            }
            if (txtluxuarytaxpercent.Text.ToString() == string.Empty)
            {
                txtluxuarytaxpercent.Text = "0";
            }
            if (txtluxuarytax.Text.ToString() == string.Empty)
            {
                txtluxuarytax.Text = "0";
            }
            if (txtroundofamt.Text.ToString() == string.Empty)
            {
                txtroundofamt.Text = "0";
            }
            if (txtdiscounttaxamt.Text.ToString() == string.Empty)
            {
                txtdiscounttaxamt.Text = "0";
            }
            if (txtnondiscounttaxamt.Text.ToString() == string.Empty)
            {
                txtnondiscounttaxamt.Text = "0";
            }
            if (txtdiscounttaxper.Text.ToString() == string.Empty)
            {
                txtdiscounttaxper.Text = "0";
            }
            if (txtnondiscounttaxper.Text.ToString() == string.Empty)
            {
                txtnondiscounttaxper.Text = "0";
            }
            if (txtservicechargeamt.Text.ToString() == string.Empty)
            {
                txtservicechargeamt.Text = "0";
            }
            if (txtluxuarytaxpercent.Text.ToString() == string.Empty)
            {
                txtluxuarytaxpercent.Text = "0";
            }
            if (txtgrandtotal.Text.ToString() == string.Empty)
            {
                txtgrandtotal.Text = "0";
            }
            if (txteffectiveratecurr.Text.ToString() == string.Empty)
            {
                txteffectiveratecurr.Text = "0";
            }
            if (txteffectiveratepre.Text.ToString() == string.Empty)
            {
                txteffectiveratepre.Text = "0";
            }
            #endregion Fill Zero
            SqlParameter[] parmaTotal = { 
                                      new SqlParameter("@INVOICEID",InvoiceID),
                                      new SqlParameter("@TAXABLEAMT",txttaxableamount.Text.Trim()),
                                      new SqlParameter("@NONTAXABLEAMT",txtnontaxableamount.Text.Trim()),
                                      new SqlParameter("@EXPREIMURSEMENTAMT","0"),
                                      new SqlParameter("@TOTALAMTBEFTAX",txttotalamount.Text.Trim()),
                                      new SqlParameter("@SERVICETAXRATE",txtservicetaxpercent.Text.Trim()),
                                      new SqlParameter("@SERVICETAXAMT",txtservicetax.Text.Trim()),
                                      new SqlParameter("@EDUCESS1RATE",txtpecesspercent.Text.Trim()),
                                      new SqlParameter("@EDUCESS1AMT",txtpecess.Text.Trim()),
                                      new SqlParameter("@EDUCESS2RATE",txthecesspercent.Text.Trim()),
                                      new SqlParameter("@EDUCESS2AMT",txthecess.Text.Trim()),
                                      new SqlParameter("@LUXURYTAXRATE",txtluxuarytaxpercent.Text.Trim()),
                                      new SqlParameter("@LUXURYTAXAMT",txtluxuarytax.Text.Trim()),
                                      new SqlParameter("@ROUNDOFFAMT",txtroundofamt.Text.Trim()),
                                      new SqlParameter("@EDITEDROUNDOFFAMT","0"),
                                      new SqlParameter("@TAXDISCOUNT",txtdiscounttaxamt.Text.Trim()),
                                      new SqlParameter("@NONTAXDISCOUNT",txtnondiscounttaxamt.Text.Trim()),
                                      new SqlParameter("@TAXDISRATE",txtdiscounttaxper.Text.Trim()),
                                      new SqlParameter("@NONTAXDISRATE",txtnondiscounttaxper.Text.Trim()),
                                      new SqlParameter("@ABTMNTAMT","0"),
                                      new SqlParameter("@SERVICECHARGEAMT",txtservicechargeamt.Text.Trim()),
                                      new SqlParameter("@VATAMT",txtluxuarytaxpercent.Text.Trim()),
                                      new SqlParameter("@TOTALAMT",txtgrandtotal.Text.Trim()),
                                      new SqlParameter("@CURRENTNETTDS","0"),
                                      new SqlParameter("@CURRENTNETAMTPAYABLE","0"),
                                      new SqlParameter("@CURRENTEFFRATE",txteffectiveratecurr.Text.Trim()),
                                      new SqlParameter("@PREVIOUSNETTDS","0"),
                                      new SqlParameter("@PREVIOUSNETAMTPAYABLE","0"),
                                      new SqlParameter("@PREVIOUSEFFRATE",txteffectiveratepre.Text.Trim()),
                                      new SqlParameter("@NETTDS","0"),
                                      new SqlParameter("@NETAMTPAYABLE","0"),
                                     };
            DU.executeprocedure("USP_IVC_SaveInvoiceTotal", parmaTotal);
        }
    }
    protected void BindChildDetailEdit()
    {
        if (Request.QueryString != null)
        {
            ViewState["INVOICEID"] = Request.QueryString["INVOICEID"].ToString();
            string strsql = "select * from IVC_INVOICE_CHILD where invoiceid=" + ViewState["INVOICEID"].ToString();

            // tblmain.Rows.Clear();

            DataTable dtChildInfo = DU.getdata(strsql);
            if (dtChildInfo.Rows.Count > 0)
            {
                BindService(ddlservice1);
                //BindService(ddlservice2);
                int countRow = tblmain.Rows.Count;
                int loopcount = dtChildInfo.Rows.Count - countRow;

                strsql = "select serviceid,LEDGERID from IVC_SERVICEMASTER";
                DataTable dtLedgid = DU.getdata(strsql);
                DataView dtvwLedgid = dtLedgid.DefaultView;

                if (dtChildInfo.Rows.Count == 1)
                {
                    #region /*Bind data from child table if only one row is returning */
                    HiddenField hidChildID = (HiddenField)tblmain.Rows[0].FindControl("hidChildID1");
                    HiddenField hidserviceLid = (HiddenField)tblmain.Rows[0].FindControl("hidserviceLid1");
                    DropDownList service = (DropDownList)tblmain.Rows[0].FindControl("ddlservice1");
                    TextBox adddesc = (TextBox)tblmain.Rows[0].FindControl("txtdescriptionctrl1");
                    TextBox yearref = (TextBox)tblmain.Rows[0].FindControl("txtyearrefctrl1");
                    TextBox uom = (TextBox)tblmain.Rows[0].FindControl("txtuomctrl1");
                    TextBox unit2 = (TextBox)tblmain.Rows[0].FindControl("txtunit2ctrl1");
                    TextBox unit3 = (TextBox)tblmain.Rows[0].FindControl("txtunit3ctrl1");
                    TextBox unit4 = (TextBox)tblmain.Rows[0].FindControl("txtunit4ctrl1");
                    TextBox Unit0 = (TextBox)tblmain.Rows[0].FindControl("txtunitrate1ctrl1");
                    TextBox Rate = (TextBox)tblmain.Rows[0].FindControl("txtunitrate2ctrl1");
                    TextBox grossamt = (TextBox)tblmain.Rows[0].FindControl("txtgrossamtctrl1");
                    TextBox discountper = (TextBox)tblmain.Rows[0].FindControl("txtdiscount1ctrl1");
                    TextBox discountamt = (TextBox)tblmain.Rows[0].FindControl("txtdiscount2ctrl1");
                    TextBox serchvper = (TextBox)tblmain.Rows[0].FindControl("txtservicecharge1crtl1");
                    TextBox serchvamt = (TextBox)tblmain.Rows[0].FindControl("txtservicecharge2crtl1");
                    TextBox totalamt = (TextBox)tblmain.Rows[0].FindControl("txtservicecharge3crtl1");
                    TextBox servicetax_h = (TextBox)tblmain.Rows[0].FindControl("txtserviceTax1crtl1");
                    TextBox pecess_h = (TextBox)tblmain.Rows[0].FindControl("txtserviceTax2crtl1");
                    TextBox hecess_h = (TextBox)tblmain.Rows[0].FindControl("txtserviceTax3crtl1");
                    TextBox luxuary_h = (TextBox)tblmain.Rows[0].FindControl("txtserviceTax4crtl1");
                    TextBox vat_h = (TextBox)tblmain.Rows[0].FindControl("txtserviceTax5crtl1");
                    TextBox GrandTotal_h = (TextBox)tblmain.Rows[0].FindControl("txtTaxincGrandTotalctrl1");

                    dtvwLedgid.RowFilter = "ServiceID =" + dtChildInfo.Rows[0]["SERVICEID"].ToString();
                    if (dtvwLedgid.Count > 0)
                    {
                        DataTable dtledg = dtvwLedgid.ToTable();
                        hidserviceLid.Value = dtledg.Rows[0]["LEDGERID"].ToString();
                    }
                    if (dtChildInfo.Rows[0]["INVOICECHILDID"].ToString() != string.Empty)
                    {
                        hidChildID.Value = dtChildInfo.Rows[0]["INVOICECHILDID"].ToString();
                    }
                    if (dtChildInfo.Rows[0]["SERVICEID"].ToString() != string.Empty)
                    {
                        service.SelectedValue = service.Items.FindByValue(Convert.ToString(dtChildInfo.Rows[0]["SERVICEID"])).Value;
                    }
                    if (dtChildInfo.Rows[0]["ADDDESC"].ToString() != string.Empty)
                    {
                        adddesc.Text = dtChildInfo.Rows[0]["ADDDESC"].ToString();
                    }
                    if (dtChildInfo.Rows[0]["YEARREF"].ToString() != string.Empty)
                    {
                        yearref.Text = dtChildInfo.Rows[0]["YEARREF"].ToString();
                    }
                    if (dtChildInfo.Rows[0]["UOM"].ToString() != string.Empty)
                    {
                        uom.Text = dtChildInfo.Rows[0]["UOM"].ToString();
                    }
                    if (dtChildInfo.Rows[0]["UNIT2"].ToString() != string.Empty)
                    {
                        unit2.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[0]["UNIT2"].ToString()));
                    }
                    if (dtChildInfo.Rows[0]["UNIT3"].ToString() != string.Empty)
                    {
                        unit3.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[0]["UNIT3"].ToString()));
                    }
                    if (dtChildInfo.Rows[0]["UNIT4"].ToString() != string.Empty)
                    {
                        unit4.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[0]["UNIT4"].ToString()));
                    }
                    if (dtChildInfo.Rows[0]["UNIT"].ToString() != string.Empty)
                    {
                        Unit0.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[0]["UNIT"].ToString()));
                    }
                    if (dtChildInfo.Rows[0]["RATE"].ToString() != string.Empty)
                    {
                        Rate.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[0]["RATE"].ToString()));
                    }
                    if (dtChildInfo.Rows[0]["TAXABLEAMT"].ToString() != string.Empty)
                    {
                        grossamt.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[0]["TAXABLEAMT"].ToString()));
                    }
                    if (dtChildInfo.Rows[0]["DISCOUNTRATE"].ToString() != string.Empty)
                    {
                        discountper.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[0]["DISCOUNTRATE"].ToString()));
                    }
                    if (dtChildInfo.Rows[0]["DISCOUNT"].ToString() != string.Empty)
                    {
                        discountamt.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[0]["DISCOUNT"].ToString()));
                    }
                    if (dtChildInfo.Rows[0]["SERVICETAXAMT"].ToString() != string.Empty)
                    {
                        servicetax_h.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[0]["SERVICETAXAMT"].ToString()));
                    }
                    if (dtChildInfo.Rows[0]["CESS1AMT"].ToString() != string.Empty)
                    {
                        pecess_h.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[0]["CESS1AMT"].ToString()));
                    }
                    if (dtChildInfo.Rows[0]["CESS2AMT"].ToString() != string.Empty)
                    {
                        hecess_h.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[0]["CESS2AMT"].ToString()));
                    }
                    if (dtChildInfo.Rows[0]["LTAMT"].ToString() != string.Empty)
                    {
                        luxuary_h.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[0]["LTAMT"].ToString()));
                    }
                    if (dtChildInfo.Rows[0]["VATAMT"].ToString() != string.Empty)
                    {
                        vat_h.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[0]["VATAMT"].ToString()));
                    }
                    if (dtChildInfo.Rows[0]["TOTALAMT"].ToString() != string.Empty)
                    {
                        GrandTotal_h.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[0]["TOTALAMT"].ToString()));
                        totalamt.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[0]["TOTALAMT"].ToString()));
                    }
                    if (dtChildInfo.Rows[0]["SERVICECHARGERATE"].ToString() != string.Empty)
                    {
                        serchvper.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[0]["SERVICECHARGERATE"].ToString()));
                    }
                    if (dtChildInfo.Rows[0]["SERVICECHARGEAMT"].ToString() != string.Empty)
                    {
                        serchvamt.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[0]["SERVICECHARGEAMT"].ToString()));
                    }
                    GetDynamicColumn();
                    #endregion
                }
                else
                {
                    for (int i = 1; i <= dtChildInfo.Rows.Count - 1; i++)
                    {
                        ADDMoreRow();
                        GetDynamicColumn();
                    }
                    try
                    {
                        #region /* Bind data from child table if more than one row is returning */
                        for (int i = 1; i <= dtChildInfo.Rows.Count; i++)
                        {
                            HiddenField hidserviceLid = (HiddenField)tblmain.Rows[i].FindControl("hidserviceLid" + i.ToString());
                            HiddenField hidChildID = (HiddenField)tblmain.Rows[i].FindControl("hidChildID" + i.ToString());
                            DropDownList service = (DropDownList)tblmain.Rows[i].FindControl("ddlservice" + i.ToString());
                            TextBox adddesc = (TextBox)tblmain.Rows[i].FindControl("txtdescriptionctrl" + i.ToString());
                            TextBox yearref = (TextBox)tblmain.Rows[i].FindControl("txtyearrefctrl" + i.ToString());
                            TextBox uom = (TextBox)tblmain.Rows[i].FindControl("txtuomctrl" + i.ToString());
                            TextBox unit2 = (TextBox)tblmain.Rows[i].FindControl("txtunit2ctrl" + i.ToString());
                            TextBox unit3 = (TextBox)tblmain.Rows[i].FindControl("txtunit3ctrl" + i.ToString());
                            TextBox unit4 = (TextBox)tblmain.Rows[i].FindControl("txtunit4ctrl" + i.ToString());
                            TextBox Unit0 = (TextBox)tblmain.Rows[i].FindControl("txtunitrate1ctrl" + i.ToString());
                            TextBox Rate = (TextBox)tblmain.Rows[i].FindControl("txtunitrate2ctrl" + i.ToString());
                            TextBox grossamt = (TextBox)tblmain.Rows[i].FindControl("txtgrossamtctrl" + i.ToString());
                            TextBox discountper = (TextBox)tblmain.Rows[i].FindControl("txtdiscount1ctrl" + i.ToString());
                            TextBox discountamt = (TextBox)tblmain.Rows[i].FindControl("txtdiscount2ctrl" + i.ToString());
                            TextBox serchvper = (TextBox)tblmain.Rows[i].FindControl("txtservicecharge1crtl" + i.ToString());
                            TextBox serchvamt = (TextBox)tblmain.Rows[i].FindControl("txtservicecharge2crtl" + i.ToString());
                            TextBox totalamt = (TextBox)tblmain.Rows[i].FindControl("txtservicecharge3crtl" + i.ToString());
                            TextBox servicetax_h = (TextBox)tblmain.Rows[i].FindControl("txtserviceTax1crtl" + i.ToString());
                            TextBox pecess_h = (TextBox)tblmain.Rows[i].FindControl("txtserviceTax2crtl" + i.ToString());
                            TextBox hecess_h = (TextBox)tblmain.Rows[i].FindControl("txtserviceTax3crtl" + i.ToString());
                            TextBox luxuary_h = (TextBox)tblmain.Rows[i].FindControl("txtserviceTax4crtl" + i.ToString());
                            TextBox vat_h = (TextBox)tblmain.Rows[i].FindControl("txtserviceTax5crtl" + i.ToString());
                            TextBox GrandTotal_h = (TextBox)tblmain.Rows[i].FindControl("txtTaxincGrandTotalctrl" + i.ToString());

                            dtvwLedgid.RowFilter = "ServiceID =" + dtChildInfo.Rows[i - 1]["SERVICEID"].ToString();
                            if (dtvwLedgid.Count > 0)
                            {
                                DataTable dtledg = dtvwLedgid.ToTable();
                                hidserviceLid.Value = dtledg.Rows[0]["LEDGERID"].ToString();
                            }
                            if (dtChildInfo.Rows[i - 1]["INVOICECHILDID"].ToString() != string.Empty)
                            {
                                hidChildID.Value = dtChildInfo.Rows[i - 1]["INVOICECHILDID"].ToString();
                            }
                            if (dtChildInfo.Rows[i - 1]["SERVICEID"].ToString() != string.Empty)
                            {
                                service.SelectedValue = service.Items.FindByValue(Convert.ToString(dtChildInfo.Rows[i - 1]["SERVICEID"])).Value;
                            }
                            if (dtChildInfo.Rows[i - 1]["ADDDESC"].ToString() != string.Empty)
                            {
                                adddesc.Text = dtChildInfo.Rows[i - 1]["ADDDESC"].ToString();
                            }
                            if (dtChildInfo.Rows[i - 1]["YEARREF"].ToString() != string.Empty)
                            {
                                yearref.Text = dtChildInfo.Rows[i - 1]["YEARREF"].ToString();
                            }
                            if (dtChildInfo.Rows[i - 1]["UOM"].ToString() != string.Empty)
                            {
                                uom.Text = dtChildInfo.Rows[i - 1]["UOM"].ToString();
                            }
                            if (dtChildInfo.Rows[i - 1]["UNIT2"].ToString() != string.Empty)
                            {
                                unit2.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[i - 1]["UNIT2"].ToString()));
                            }
                            if (dtChildInfo.Rows[i - 1]["UNIT3"].ToString() != string.Empty)
                            {
                                unit3.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[i - 1]["UNIT3"].ToString()));
                            }
                            if (dtChildInfo.Rows[i - 1]["UNIT4"].ToString() != string.Empty)
                            {
                                unit4.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[i - 1]["UNIT4"].ToString()));
                            }
                            if (dtChildInfo.Rows[i - 1]["UNIT"].ToString() != string.Empty)
                            {
                                Unit0.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[i - 1]["UNIT"].ToString()));
                            }
                            if (dtChildInfo.Rows[i - 1]["RATE"].ToString() != string.Empty)
                            {
                                Rate.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[i - 1]["RATE"].ToString()));
                            }
                            if (dtChildInfo.Rows[i - 1]["TAXABLEAMT"].ToString() != string.Empty)
                            {
                                grossamt.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[i - 1]["TAXABLEAMT"].ToString()));
                            }
                            if (dtChildInfo.Rows[i - 1]["DISCOUNTRATE"].ToString() != string.Empty)
                            {
                                discountper.Text = dtChildInfo.Rows[i - 1]["DISCOUNTRATE"].ToString();
                            }
                            if (dtChildInfo.Rows[i - 1]["DISCOUNT"].ToString() != string.Empty)
                            {
                                discountamt.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[i - 1]["DISCOUNT"].ToString()));
                            }
                            if (dtChildInfo.Rows[i - 1]["SERVICETAXAMT"].ToString() != string.Empty)
                            {
                                servicetax_h.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[i - 1]["SERVICETAXAMT"].ToString()));
                            }
                            if (dtChildInfo.Rows[i - 1]["CESS1AMT"].ToString() != string.Empty)
                            {
                                pecess_h.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[i - 1]["CESS1AMT"].ToString()));
                            }
                            if (dtChildInfo.Rows[i - 1]["CESS2AMT"].ToString() != string.Empty)
                            {
                                hecess_h.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[i - 1]["CESS2AMT"].ToString()));
                            }
                            if (dtChildInfo.Rows[i - 1]["LTAMT"].ToString() != string.Empty)
                            {
                                luxuary_h.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[i - 1]["LTAMT"].ToString()));
                            }
                            if (dtChildInfo.Rows[i - 1]["VATAMT"].ToString() != string.Empty)
                            {
                                vat_h.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[i - 1]["VATAMT"].ToString()));
                            }
                            if (dtChildInfo.Rows[i - 1]["TOTALAMT"].ToString() != string.Empty)
                            {
                                GrandTotal_h.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[i - 1]["TOTALAMT"].ToString()));
                                totalamt.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[i - 1]["TOTALAMT"].ToString()));
                            }
                            if (dtChildInfo.Rows[i - 1]["SERVICECHARGERATE"].ToString() != string.Empty)
                            {
                                serchvper.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[i - 1]["SERVICECHARGERATE"].ToString()));
                            }
                            if (dtChildInfo.Rows[i - 1]["SERVICECHARGEAMT"].ToString() != string.Empty)
                            {
                                serchvamt.Text = string.Format("{0:0.00}", Convert.ToDouble(dtChildInfo.Rows[i - 1]["SERVICECHARGEAMT"].ToString()));
                            }
                        }
                        #endregion
                    }
                    catch (Exception ex)
                    {
                        lblMsg.Text = ex.Message.ToString();
                    }
                }
            }
        }
    }
    protected void BindDetailsForEdit()
    {
        if (Request.QueryString != null)
        {
            string strsql = "";
            ViewState["INVOICEID"] = Request.QueryString["INVOICEID"].ToString();
            ViewState["TRANID"] = Request.QueryString["TRANID"].ToString();

            #region /*Binding detail from Main Tablle*/
            strsql = "select * from IVC_INVOICE_MAIN where invoiceid=" + ViewState["INVOICEID"].ToString();
            DataTable dtGetMainData = DU.getdata(strsql);
            if (dtGetMainData.Rows.Count > 0)
            {
                strsql = "select FormatName,REFERENCENUMBER from ivc_TRANSACTIONFORMAT where formatid=" + dtGetMainData.Rows[0]["FORMATID"].ToString();
                DataTable dtFormatid = DU.getdata(strsql);
                string strFormatname = dtFormatid.Rows[0]["FORMATNAME"].ToString();
                if (dtFormatid.Rows.Count > 0)
                {
                    ddlFormat.SelectedValue = ddlFormat.Items.FindByText(strFormatname).Value;
                    //  txtrefno.Text = Convert.ToInt32(dtFormatid.Rows[0]["REFERENCENUMBER"]).ToString();
                }

                strsql = "select LName from ACC_LEDMAST where LID=" + dtGetMainData.Rows[0]["CUSTOMERLEDGERID"].ToString();
                DataTable dtCustName = DU.getdata(strsql);
                string strCustName = dtCustName.Rows[0]["LName"].ToString();

                SqlParameter[] parmaled = { 
                                           new SqlParameter("@LID",dtGetMainData.Rows[0]["CUSTOMERLEDGERID"].ToString())
                                          };
                DataTable dtledadd = DU.getdatausingprocedure("USP_ACC_GetLedgerAddressDetails", parmaled);

                if (dtledadd.Rows.Count > 0)
                {
                    txtaddress.Text = dtledadd.Rows[0]["LNAME"].ToString() + " " + dtledadd.Rows[0]["ADDRESS1"].ToString() + " " + dtledadd.Rows[0]["ADDRESS2"].ToString() + " " + dtledadd.Rows[0]["ADDRESS3"].ToString() + " " + dtledadd.Rows[0]["PLACE"].ToString() + " " + dtledadd.Rows[0]["STATENAME"].ToString();
                }
                if (dtCustName.Rows.Count > 0)
                {
                    ddlCustomer.SelectedValue = ddlCustomer.Items.FindByText(strCustName).Value;
                }
                if (dtGetMainData.Rows[0]["PREPAREDBY"].ToString() != string.Empty)
                {
                    txtpreparedby.Text = dtGetMainData.Rows[0]["PREPAREDBY"].ToString();
                }
                if (dtGetMainData.Rows[0]["SERVICEDBY"].ToString() != string.Empty)
                {
                    txtservicedby.Text = dtGetMainData.Rows[0]["SERVICEDBY"].ToString();
                }
                if (dtGetMainData.Rows[0]["CHECKEDBY"].ToString() != string.Empty)
                {
                    txtcheckedby.Text = dtGetMainData.Rows[0]["CHECKEDBY"].ToString();
                }
                if (dtGetMainData.Rows[0]["COMMENTS"].ToString() != string.Empty)
                {
                    txtremarks.Text = dtGetMainData.Rows[0]["COMMENTS"].ToString();
                }
                if (dtGetMainData.Rows[0]["SERVICEORDER"].ToString() != string.Empty)
                {
                    txtotherref.Text = dtGetMainData.Rows[0]["SERVICEORDER"].ToString();
                }
            #endregion
                #region /*Binding Detail from Total Table*/
                strsql = "select * from IVC_INVOICE_TOTAL where INVOICEID=" + ViewState["INVOICEID"].ToString();
                DataTable dtInvoiceTotal = DU.getdata(strsql);
                if (dtInvoiceTotal.Rows.Count > 0)
                {

                    if (dtInvoiceTotal.Rows[0]["TAXABLEAMT"].ToString() != string.Empty)
                    {
                        txttaxableamount.Text = string.Format("{0:0.00}", Convert.ToDouble(dtInvoiceTotal.Rows[0]["TAXABLEAMT"].ToString()));
                    }
                    if (dtInvoiceTotal.Rows[0]["NONTAXABLEAMT"].ToString() != string.Empty)
                    {
                        txtnontaxableamount.Text = string.Format("{0:0.00}", Convert.ToDouble(dtInvoiceTotal.Rows[0]["NONTAXABLEAMT"].ToString()));
                    }
                    if (dtInvoiceTotal.Rows[0]["TOTALAMTBEFTAX"].ToString() != string.Empty)
                    {
                        txttotalamount.Text = string.Format("{0:0.00}", Convert.ToDouble(dtInvoiceTotal.Rows[0]["TOTALAMTBEFTAX"].ToString()));
                    }
                    if (dtInvoiceTotal.Rows[0]["SERVICETAXRATE"].ToString() != string.Empty)
                    {
                        txtservicetaxpercent.Text = string.Format("{0:0.00}", Convert.ToDouble(dtInvoiceTotal.Rows[0]["SERVICETAXRATE"].ToString()));
                    }
                    if (dtInvoiceTotal.Rows[0]["SERVICETAXAMT"].ToString() != string.Empty)
                    {
                        txtservicetax.Text = string.Format("{0:0.00}", Convert.ToDouble(dtInvoiceTotal.Rows[0]["SERVICETAXAMT"].ToString()));
                    }
                    if (dtInvoiceTotal.Rows[0]["EDUCESS1RATE"].ToString() != string.Empty)
                    {
                        txtpecesspercent.Text = string.Format("{0:0.00}", Convert.ToDouble(dtInvoiceTotal.Rows[0]["EDUCESS1RATE"].ToString()));
                    }
                    if (dtInvoiceTotal.Rows[0]["EDUCESS1AMT"].ToString() != string.Empty)
                    {
                        txtpecess.Text = string.Format("{0:0.00}", Convert.ToDouble(dtInvoiceTotal.Rows[0]["EDUCESS1AMT"].ToString()));
                    }
                    if (dtInvoiceTotal.Rows[0]["EDUCESS2RATE"].ToString() != string.Empty)
                    {
                        txthecesspercent.Text = string.Format("{0:0.00}", Convert.ToDouble(dtInvoiceTotal.Rows[0]["EDUCESS2RATE"].ToString()));
                    }
                    if (dtInvoiceTotal.Rows[0]["EDUCESS2AMT"].ToString() != string.Empty)
                    {
                        txthecess.Text = string.Format("{0:0.00}", Convert.ToDouble(dtInvoiceTotal.Rows[0]["EDUCESS2AMT"].ToString()));
                    }
                    if (dtInvoiceTotal.Rows[0]["LUXURYTAXRATE"].ToString() != string.Empty)
                    {
                        txtluxuarytaxpercent.Text = string.Format("{0:0.00}", Convert.ToDouble(dtInvoiceTotal.Rows[0]["LUXURYTAXRATE"].ToString()));
                        hidluxuarytaxpercent.Value = string.Format("{0:0.00}", Convert.ToDouble(dtInvoiceTotal.Rows[0]["LUXURYTAXRATE"].ToString()));
                    }
                    if (dtInvoiceTotal.Rows[0]["LUXURYTAXAMT"].ToString() != string.Empty)
                    {
                        txtluxuarytax.Text = string.Format("{0:0.00}", Convert.ToDouble(dtInvoiceTotal.Rows[0]["LUXURYTAXAMT"].ToString()));
                    }
                    if (dtInvoiceTotal.Rows[0]["ROUNDOFFAMT"].ToString() != string.Empty)
                    {
                        txtroundofamt.Text = string.Format("{0:0.00}", Convert.ToDouble(dtInvoiceTotal.Rows[0]["ROUNDOFFAMT"].ToString()));
                    }
                    if (dtInvoiceTotal.Rows[0]["TAXDISCOUNT"].ToString() != string.Empty)
                    {
                        txtdiscounttaxamt.Text = string.Format("{0:0.00}", Convert.ToDouble(dtInvoiceTotal.Rows[0]["TAXDISCOUNT"].ToString()));
                    }
                    if (dtInvoiceTotal.Rows[0]["NONTAXDISCOUNT"].ToString() != string.Empty)
                    {
                        txtnondiscounttaxamt.Text = string.Format("{0:0.00}", Convert.ToDouble(dtInvoiceTotal.Rows[0]["NONTAXDISCOUNT"].ToString()));
                    }
                    if (dtInvoiceTotal.Rows[0]["TAXDISRATE"].ToString() != string.Empty)
                    {
                        txtdiscounttaxper.Text = string.Format("{0:0.00}", Convert.ToDouble(dtInvoiceTotal.Rows[0]["TAXDISRATE"].ToString()));
                    }
                    if (dtInvoiceTotal.Rows[0]["NONTAXDISRATE"].ToString() != string.Empty)
                    {
                        txtnondiscounttaxper.Text = string.Format("{0:0.00}", Convert.ToDouble(dtInvoiceTotal.Rows[0]["NONTAXDISRATE"].ToString()));
                    }
                    if (dtInvoiceTotal.Rows[0]["SERVICETAXAMT"].ToString() != string.Empty)
                    {
                        txtservicechargeamt.Text = string.Format("{0:0.00}", Convert.ToDouble(dtInvoiceTotal.Rows[0]["SERVICETAXAMT"].ToString()));
                    }
                    if (dtInvoiceTotal.Rows[0]["VATAMT"].ToString() != string.Empty)
                    {
                        txtluxuarytaxpercent.Text = string.Format("{0:0.00}", Convert.ToDouble(dtInvoiceTotal.Rows[0]["VATAMT"].ToString()));
                    }
                    if (dtInvoiceTotal.Rows[0]["TOTALAMT"].ToString() != string.Empty)
                    {
                        txtgrandtotal.Text = string.Format("{0:0.00}", Convert.ToDouble(dtInvoiceTotal.Rows[0]["TOTALAMT"].ToString()));
                    }
                    if (dtInvoiceTotal.Rows[0]["CURRENTEFFRATE"].ToString() != string.Empty)
                    {
                        txteffectiveratecurr.Text = string.Format("{0:0.00}", Convert.ToDouble(dtInvoiceTotal.Rows[0]["CURRENTEFFRATE"].ToString()));
                    }
                    if (dtInvoiceTotal.Rows[0]["PREVIOUSEFFRATE"].ToString() != string.Empty)
                    {
                        txteffectiveratepre.Text = string.Format("{0:0.00}", Convert.ToDouble(dtInvoiceTotal.Rows[0]["PREVIOUSEFFRATE"].ToString()));
                    }
                    if (dtInvoiceTotal.Rows[0]["SERVICECHARGEAMT"].ToString() != string.Empty)
                    {
                        txtservicechargeamt.Text = string.Format("{0:0.00}", Convert.ToDouble(dtInvoiceTotal.Rows[0]["SERVICECHARGEAMT"].ToString()));
                    }

                }
                #endregion
                #region /*Binding details from Acc_Vouchers Table*/

                strsql = "select NARRATION,CRDAYS,VOUDATE,VOUNO,FINYEAR,REFNO from Acc_VOuchers where tranid=" + ViewState["TRANID"].ToString();
                DataTable dtDtlofVoucher = DU.getdata(strsql);
                if (dtDtlofVoucher.Rows.Count > 0)
                {
                    if (dtDtlofVoucher.Rows[0]["NARRATION"].ToString() != string.Empty)
                    {
                        txtnarration.Text = dtDtlofVoucher.Rows[0]["NARRATION"].ToString();
                    }
                    if (dtDtlofVoucher.Rows[0]["CRDAYS"].ToString() != string.Empty)
                    {
                        txtcrdays.Text = dtDtlofVoucher.Rows[0]["CRDAYS"].ToString();
                    }
                    if (dtDtlofVoucher.Rows[0]["VOUDATE"].ToString() != string.Empty)
                    {
                        txtdate.Text = dtDtlofVoucher.Rows[0]["VOUDATE"].ToString();
                    }
                    if (dtDtlofVoucher.Rows[0]["VOUNO"].ToString() != string.Empty)
                    {
                        txtbillno.Text = dtDtlofVoucher.Rows[0]["VOUNO"].ToString();
                    }
                    if (dtDtlofVoucher.Rows[0]["REFNO"].ToString() != string.Empty)
                    {
                        txtrefno.Text = dtDtlofVoucher.Rows[0]["REFNO"].ToString();
                    }
                    ViewState["finyear"] = dtDtlofVoucher.Rows[0]["FINYEAR"].ToString();

                    if (ddlFormat.SelectedValue != "0")
                    {
                        string strFinyear = "";
                        SqlParameter[] sparam =
                                  {
                                     new SqlParameter("@TFOID",ddlFormat.SelectedValue),
                                     new SqlParameter ("@FINYEAR", dtDtlofVoucher.Rows[0]["FINYEAR"].ToString())
                                  };
                        DataTable DTVoucher = DU.getdatausingprocedure("USP_IVC_GetMaxVoucherNo", sparam);
                        if (DTVoucher.Rows.Count > 0)
                        {
                            txtPrefix.Text = DTVoucher.Rows[0]["prefix"].ToString();
                            txtSuffix.Text = DTVoucher.Rows[0]["suffix"].ToString();
                        }
                    }
                    SqlParameter[] parma = { 
                                             new SqlParameter("@FormatId", ddlFormat.SelectedValue) 
                                           };
                    DataTable dttrantype = DU.getdatausingprocedure("Usp_IVC_GetTranType", parma);
                    if (dttrantype.Rows.Count > 0)
                    {
                        hidTranType.Value = dttrantype.Rows[0]["TranType"].ToString();
                    }
                }
                #endregion
            }
        }
    }
}